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

创建了将人名用作键的字典,输入姓名查找值,返回错误。
代码:

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
一只斗牛犬
浏览 921回答 2
2回答

白衣染霜花

你用的 python2. 使用 raw_input; name=raw_input('Name:')

临摹微笑

我copy你的code一切正常,沒有異常,我用的是qpython3
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python