如何解决 NameError: name 'xx' is not defined?

奥利弗·杨

我在学python,按照书上代码写的逻辑,想看看运行结果,代码如下,但是输出报错NameError: name 'pr' is not defined

代码显示如下:

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()

错误信息如下:

Traceback (most recent call last):
  File "/Users/oliver/Desktop/test.py", line 22, in <module>
    choice=input(pr).strip()[0].lower()
NameError: name 'pr' is not defined
可以罗伯特

您的代码中有两个错误,

  1. 方法中的逻辑showmenu需要缩进
  2. _name_=='_main_'->__name__ == "__main__"
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()
 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何解决错误 NameError: name 'SparkConf' is not defined in pycharm

如何修复“NameError: name 'context' is not defined”

如何修复“NameError: name 'color' is not defined”?

如何正确配置python 3的使用避免NameError: name '' is not defined

如何解决此“ NameError”?

如何解决:NameError: name 'on_key_release' 未定义?(Python)

导入 kafka 给出“NameError: name 'true' is not defined”

NameError: ("name 'true' is not defined", 'occurred at index 0')

如何在 python 中关于海面温度的图中修复“NameError: name 'cartopy' is not defined'?

iPython调试器引发`NameError:name ... not defined`

不明白这个“NameError: name 'self' is not defined”错误

在类中导入模块时出现“NameError: name [module] is not defined”

“NameError: name 'update' is not defined” 使用数据表时出错

Scrapy:在scrapy 中使用SQLAlchemy 有“NameError: name 'connection' is not defined”

为什么我会收到“NameError: name 'self' is not defined”?

我有一个错误:NameError: name 'GameDisplay' is not defined

错误“NameError: name 'self' is not defined”即使我声明了“self”

raise self._value NameError: name 'global_df' is not defined

无法在 18.04 中添加 PPA 密钥:NameError: name 'KUrl' is not defined

试图从父类继承变量“NameError: name 'r' is not defined”

Django连接mysql问题,NameError: name '_mysql' is not defined

在 Django 中使用类型提示 Any - NameError: name 'Any' is not defined

为什么python在定义时返回“NameError:name 'correct' is not defined?

NameError: name 'i' is not defined - 为什么会发生这种情况?

当我在请求输入时尝试中断 while 循环时,如何克服“NameError: name 'x' is not defined”?

如何解决这个“Uncaught ReferenceError: $ is not defined”

如何解决““ [xX] [mM] [lL]”不允许“的错误

如何解决-192.168.XX连接关闭?

如何解决“ NameError:名称'model'未定义”错误?