免费注册 查看新帖 |

Chinaunix

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

python 程序报错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-02 15:45 |只看该作者 |倒序浏览
我完全按照教科书上写的程序,怎么执行不了的,程序如下 :
#!/usr/bin/env python

stack = []

def pushit():
          stack.append(raw_input('Enter New string:').strip())
          
def popit():
          if len(stack) == 0 :
                  print 'Cannot pop from an empty stack!'
                 
          else:
                  print 'Removed [',`stack.pop()` ,']'
                 
                 
def viewstack():
          print stack
          
          
          
CMDs = {'u':pushit,'o':popit,'v':viewstack}
       
       
def showmenu():
           pr = '''
           p(U)sh
           p(O)p
           (V)iew
           (Q)uit

    Enter choice:'''
   
   
while True :
          while True:
                   try:
                           choice = raw_input(pr).strip()[0].lower()
                   except (EOFError,KeyboardInterrupt,IndexError):
                           choice = 'q'
                          
                   print '\nYou picked:[%s]' %choice
                   if choice not in 'uovq':
                             print 'Invalid option,try again'
                   else:
                             break
                             
                             
          if choice == 'q':
                    break
          CMDs[choice]()

if _name_ == '_main_':
         showmenu()  


执行之后报错:
[root@localhost python]# python stack.py
Traceback (most recent call last):
  File "stack.py", line 37, in <module>
    choice = raw_input(pr).strip()[0].lower()
NameError: name 'pr' is not defined

QQ截图20150502154417.jpg (20.49 KB, 下载次数: 43)

QQ截图20150502154417.jpg

论坛徽章:
10
天蝎座
日期:2013-09-22 22:32:23程序设计版块每日发帖之星
日期:2016-08-07 06:20:00lufei
日期:2016-06-17 17:38:40程序设计版块每日发帖之星
日期:2016-06-12 06:20:002016科比退役纪念章
日期:2016-05-31 15:47:20CU十四周年纪念徽章
日期:2016-05-27 12:24:562015年亚洲杯之阿曼
日期:2015-05-03 21:01:352015年辞旧岁徽章
日期:2015-03-03 16:54:15天蝎座
日期:2013-10-20 21:05:24程序设计版块每日发帖之星
日期:2016-08-11 06:20:00
2 [报告]
发表于 2015-05-02 21:05 |只看该作者
回复 1# jack_yanf


    while True :
          while True:
                   try:

这段代码是在showmenu下面,缩进格式不对,所以找不到pr

另外, _main_写法是错的,是__main__,前后都是2个下划线

论坛徽章:
0
3 [报告]
发表于 2015-05-02 22:15 |只看该作者
回复 2# liion631818
原先缩进有大问题,然后那个两个下划线,刚接触python不久,还不是很清楚,太谢谢你的回答了。


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP