初学python一个细小的问题

what911 2012-07-02 05:38:06
#!/user/bin/env python
stack=[]
def pushit ():
stack.append(input('Enter New string:').strip())
def popit ():
if len(stack)==0:
print('Can not pop from an empty stack!')
else:
print('Removes[',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 1>0:
while 1>0:
try:
choice=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()
这样,返回Traceback (most recent call last):
File "E:/Python/菜单驱动.py", line 24, in <module>
choice=input(pr).strip()[0].lower()
NameError: name 'pr' is not defined
请教,怎么改?
...全文
86 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiefeiying 2013-03-18
  • 打赏
  • 举报
回复
showmen函数下面包括while那段,被坑的好惨
悠亮仔 2012-07-31
  • 打赏
  • 举报
回复
谢了,缩进一直是个问题,python核心编程电子版例子上的缩进一点都不规范
what911 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Python code

#!/user/bin/env python
stack = []
def pushit():
stack.append(raw_input('Enter New string:').strip())

def popit():
if len(stack) == 0:
print('Can not pop from an empty s……
[/Quote]
双下滑...搞定.
what911 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Python code

#!/user/bin/env python
stack = []
def pushit():
stack.append(raw_input('Enter New string:').strip())

def popit():
if len(stack) == 0:
print('Can not pop from an empty s……
[/Quote]
嗯.是缩进问题.但是我改了while缩进之后提示NameError: name '_name_' is not defined
Rlay_2 2012-07-02
  • 打赏
  • 举报
回复

#!/user/bin/env python
stack = []
def pushit():
stack.append(raw_input('Enter New string:').strip())

def popit():
if len(stack) == 0:
print('Can not pop from an empty stack!')
else:
print('Removes[', 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 1 > 0:
while 1 > 0:
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()

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧