我要做的是将输入的字符串用逗号分隔并将其更改为list然后为每个列表作为键打印相关值。
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();
问题是,如果我返回'res'而不是打印它,我只获得第一个键值。
带打印的输出(如果我输入a,b):1, 2, none
返回1
答案 0 :(得分:0)
这些线条是否正确缩进?
for ml in key:
value = myDict.get(ml, "not found");
re = [];
re.append(value)
我想你想要它这样做:
re = []
for ml in key:
value = myDict.get(ml, "not found")
re.append(value)
return '-'.join(re)
BTW中没有使用分号。
答案 1 :(得分:0)
我认为这是因为您在每个循环中重置re
。尝试将re=[]
放在for循环之外。