我有一个列表/字典存储与一些像这样对应一个字:想不通这有什么错我的方法
MSG1 3027
MEMORYSPACE 3036
NEWLINE 3037
NEG48 3038
我的方法,如果用户输入单词检索号:
def retrieve_data():
insert_data()
nb = input('Choose a label: ')
for j in data:
a=j[0]
b=j[1]
if a == nb:
print(b)
else:
print('Label not in list!')
,所以我应该只得到3036但这是输出我得到当我调用方法:
Choose a label: MEMORYSPACE
Label not in list!
3036
Label not in list!
Label not in list!
任何想法,为什么它这样做?谢谢
一点点的改进在这里:'print(data.get(nb,'Label not in list!'))' – Eithos 2015-02-12 02:46:30