这个哪里出错了?运行出错了
本帖最后由 huangxiebo 于 2014-10-20 13:17 编辑people={
'alice':{
'phone': '1596',
'addr':'foo drive 23'
},
'beth':{
'phone': '1324',
'addr': 'bar street 45'
},
'huang':{
'phone': '1558',
'addr': 'dongpu'
}
}
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's %s is %s" % (name,labels,people)
>>> name=input('name:')
name:alice
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
name=input('name:')
File "<string>", line 1, in <module>
NameError: name 'alice' is not defined
>>> name=raw_input('name:')
name:alice
>>> name=eval(raw_input('name:'))
name:alice
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
name=eval(raw_input('name:'))
File "<string>", line 1, in <module>
NameError: name 'alice' is not defined
>>> alice
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
alice
NameError: name 'alice' is not defined
>>> eval(alice)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
eval(alice)
NameError: name 'alice' is not defined
>>> 不知道是不是这个意思,python2.x使用raw_input()吧
input()
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.
If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.
Consider using the raw_input() function for general input from users.
回复 1# huangxiebo
我怎么没错呢。。。我用python3写的。
回复 3# tshrr
知道原因了,python版本不同造成的。我的是3.4版本。在最后的print语句加个括号就好了:print ("%s's %s is %s" % (name,labels,people)) 回复 2# whitelotus19
知道原因了,python版本不同造成的。我的是3.4版本。在最后的print语句加个括号就好了:print ("%s's %s is %s" % (name,labels,people)) 回复 5# huangxiebo
是的,python3.x用print()函数,我看你用的print语句,以为你用的python2.x版本的。
回复 6# whitelotus19
刚开始学的python,见笑了
页:
[1]