免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2683 | 回复: 6
打印 上一主题 下一主题

这个哪里出错了?运行出错了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-10-20 11:57 |只看该作者 |倒序浏览
本帖最后由 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[key],people[name][key])

论坛徽章:
0
2 [报告]
发表于 2014-10-22 09:28 |只看该作者
  1. >>> name=input('name:')
  2. name:alice

  3. Traceback (most recent call last):
  4.   File "<pyshell#4>", line 1, in <module>
  5.     name=input('name:')
  6.   File "<string>", line 1, in <module>
  7. NameError: name 'alice' is not defined
  8. >>> name=raw_input('name:')
  9. name:alice
  10. >>> name=eval(raw_input('name:'))
  11. name:alice

  12. Traceback (most recent call last):
  13.   File "<pyshell#6>", line 1, in <module>
  14.     name=eval(raw_input('name:'))
  15.   File "<string>", line 1, in <module>
  16. NameError: name 'alice' is not defined
  17. >>> alice

  18. Traceback (most recent call last):
  19.   File "<pyshell#8>", line 1, in <module>
  20.     alice
  21. NameError: name 'alice' is not defined
  22. >>> eval(alice)

  23. Traceback (most recent call last):
  24.   File "<pyshell#9>", line 1, in <module>
  25.     eval(alice)
  26. NameError: name 'alice' is not defined
  27. >>>
复制代码
不知道是不是这个意思,python2.x使用raw_input()吧

input([prompt])
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.

论坛徽章:
0
3 [报告]
发表于 2014-10-22 11:23 |只看该作者
回复 1# huangxiebo
我怎么没错呢。。。我用python3写的。

   

论坛徽章:
0
4 [报告]
发表于 2014-10-22 18:54 |只看该作者
回复 3# tshrr


    知道原因了,python版本不同造成的。我的是3.4版本。在最后的print语句加个括号就好了:print ("%s's %s is %s" % (name,labels[key],people[name][key]))

论坛徽章:
0
5 [报告]
发表于 2014-10-22 18:54 |只看该作者
回复 2# whitelotus19


    知道原因了,python版本不同造成的。我的是3.4版本。在最后的print语句加个括号就好了:print ("%s's %s is %s" % (name,labels[key],people[name][key]))

论坛徽章:
0
6 [报告]
发表于 2014-10-22 20:05 |只看该作者
回复 5# huangxiebo

是的,python3.x用print()函数,我看你用的print语句,以为你用的python2.x版本的。
   

论坛徽章:
0
7 [报告]
发表于 2014-10-22 21:39 |只看该作者
回复 6# whitelotus19


    刚开始学的python,见笑了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP