创建了将人名用作键的字典,输入姓名查找值,返回错误。代码:
people={
'Alice': {
'phone': '6789',
'addr': 'Ruan road 23'
},
'Mary':{
'phone': '8769',
'addr': 'Foo drive 66'
},
'Jay': {
'phone': '6666',
'addr': 'Bar street 88'
}
}
labels={
'phone':'phone number',
'addr':'address'
}
name=input('Name:')
request=input('phone number(p) or address(a)?')
if request=='p':key='phone'
if request=='a':key='addr'
if name in people: print("{}'s {} is {}.".format(name,labels[key],people[name][key]))
错误:
D:\anaconda2-5.3\python.exe C:\Users\59131\.IntelliJIdea2018.2\config\plugins\python\helpers\pydev\pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 55756 --file C:/Users/59131/.IntelliJIdea2018.2/config/scratches/04dict1.py
pydev debugger: process 23116 is connecting
Connected to pydev debugger (build 182.4892.20)
Name:Jay
Traceback (most recent call last):
File "C:\Users\59131\.IntelliJIdea2018.2\config\plugins\python\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Users\59131\.IntelliJIdea2018.2\config\plugins\python\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Users\59131\.IntelliJIdea2018.2\config\plugins\python\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/Users/59131/.IntelliJIdea2018.2/config/scratches/04dict1.py", line 20, in <module>
name=input('Name:')
File "<string>", line 1, in <module>
NameError: name 'Jay' is not defined
添加回答
举报
0/150
提交
取消