为了账号安全,请及时绑定邮箱和手机立即绑定

创建一个将人名用作键的字典后,输入姓名查找值,返回错误

创建一个将人名用作键的字典后,输入姓名查找值,返回错误

一只斗牛犬 2019-02-17 06:46:36
创建了将人名用作键的字典,输入姓名查找值,返回错误。代码: 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
查看完整描述

2 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

你用的 python2. 使用 raw_input;

name=raw_input('Name:')
查看完整回答
反对 回复 2019-03-01
?
临摹微笑

TA贡献1982条经验 获得超2个赞

我copy你的code一切正常,沒有異常,我用的是qpython3

查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 920 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信