-2

what I'm trying to do is take the entered string separated by commas and change it to list then for each list as a key print the associated value.

def main():
    myDict = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5....}
    u_input = input("Enter a letter")
    myList = [x.strip() for x in u_input.split(',')]
    result = searchDict(myList)
    print(result)

def searchDict(key):
    for ml in key:
        value = myDict.get(ml, "not found")
        re = []
        re.append(value)
        print('-'.join(re)) #this one shows each value for each entered key separated by comma but it does not print it in one line also prints 'None' on the end

    #res = '-'.join(re)
    #return res //this only shows the value for the first key only even if I enter multiple letter

main();

The prob is that, if I return 'res' instead of printing it, I'm only getting the first key value.

output with print(if I enter a, b): 1, 2, none

output with return 1

Stephen Lin
  • 4,852
  • 1
  • 13
  • 26
Magna
  • 598
  • 3
  • 13
  • 23

2 Answers2

0

Are these lines indented properly?

for ml in key:
value = myDict.get(ml, "not found"); 
re = [];
re.append(value)

I think you want it to do this:

re = []
for ml in key:
    value = myDict.get(ml, "not found")    
    re.append(value)
return '-'.join(re)

BTW no semicolons used in Python.

Dan
  • 1,874
  • 1
  • 16
  • 21
0

I think it's because you reset re in every loop. Try to put re=[] outside the for loop.

Stephen Lin
  • 4,852
  • 1
  • 13
  • 26