stack.py

 1 #!/usr/bin/env python
 2 stack = []
 3 
 4 def pushit():
 5     stack.append(raw_input(' Enter New String: ').strip())
 6 
 7 def popit():
 8     if len(stack) == 0:
 9         print 'Cannot pop from an empty stack!'
10     else:
11         print 'Removed [', `stack.pop()`, ']'
12 
13 def viewstack():
14     print stack
15 
16 CMDs = {'u': pushit, 'o':popit, 'v':viewstack}
17 def showmenu():
18 
19     pr="""
20     P(U)sh
21     p(O)p
22     (V)iew
23     (Q)uit
24     
25 Enter choice:"""
26 #注意下面第一个while和上面的pr对齐
27     while True:
28 
29         while True:
30 
31             try:
32 
33                 choice = raw_input(pr).strip()[0].lower()
34             except (EOFError, KeyboardInterrupt, IndexError):
35                 choice = 'q'
36 
37             print '\n You picked: [%s]' % choice
38             if choice not in 'uovq':
39 
40                 print 'Invalid option, try again'
41             else:
42                 break
43 
44         if choice == 'q':
45 
46             break
47         CMDs[choice]()
48 if __name__ == '__main__':
49     showmenu()

NameError: name 'pr' is not defined

当出现此错误时,需注意的是将第一个while与上面的pr对齐。

posted @ 2017-11-01 00:54  xyu1  阅读(304)  评论(0编辑  收藏  举报