【问题标题】:why am i getting list is not defined error in python IDLE? [closed]为什么我在 python IDLE 中得到 list is not defined 错误? [关闭]
【发布时间】:2020-06-01 01:46:46
【问题描述】:

我创建了一个 tictactoe 类的对象,然后在其上调用了 display 方法:我收到错误消息说 List_1 未定义 块引用

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    a.display()
  File "C:/Users/Kunall/Documents/#1 Code/#Projects/tic_tac_toe_0.2.py", line 6, in display
for l in list_1:
NameError: name 'list_1' is not defined

块引用

【问题讨论】:

  • 不要发布代码图片。您在这里已经 3 年多了,是时候弄清楚如何在问题中粘贴和格式化代码了。
  • 如果你想引用类变量list_1,你需要做self.list_1——它将检查实例命名空间并回退到类命名空间。 list_1 本身只是该方法的局部变量。设计方面,我认为list_1 作为在__init__ 中初始化的实例变量更好。

标签: python list nameerror


【解决方案1】:

您应该使用self.list_1 而不是list_1,类似地使用self.uc_list

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-08
    • 2019-09-06
    • 2017-04-13
    • 1970-01-01
    • 2012-06-13
    • 2020-12-16
    • 1970-01-01
    • 2022-11-16
    相关资源
    最近更新 更多