weixin_43597661 2019-02-23 09:10 采纳率: 33.3%
浏览 77557
已采纳

python的NameError: name 'xx‘ is not defined该怎么解决?

初学python 按着书上的代码打 想看看运行过程 代码如下

stack=[]
def pushit():
    stack:append(input(' Enter New String: ').strip())
def popit():
    if len(stack)==0:
        print('Cannot 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 True:
    while True:
        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()

但是总提示NameError: name 'pr' is not defined ,该怎么解决这个问题?

  • 写回答

2条回答

  • threenewbee 2019-02-23 09:47
    关注

    def showmenu():
    pr='''
    p(U)sh
    p(O)p
    (V)iew
    (Q)uit

    Enter choice:'''
    while True:
    while True:
    这里的while缩进貌似不对,否则它不在showmenu函数内了。
    导致pr成为局部变量,而下面的代码成为全局的代码,认不到pr

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作