python-3.x - “TypeError:'NoneType' 对象不可下标”是什么意思?
问题描述
我正在 Python3 中使用 Pygame 编写蛇游戏。但是,我不断收到这个 TypeError,我不仅不知道这意味着什么,我也不知道如何修复它
我没有真正尝试过任何事情,因为我不知道它是什么或如何解决它。这是代码:
def draw(self, surface, eyes=False):
dis = self.w // self.rows
i = self.pos[0]
j = self.pos[1]
我不断收到的错误消息是:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "snake.py", line 179, in <module>
main()
File "snake.py", line 175, in main
redrawWindow(win)
File "snake.py", line 127, in redrawWindow
snack.draw(surface)
File "snake.py", line 25, in draw
i = self.pos[0]
TypeError: 'NoneType' object is not subscriptable
解决方案
当您尝试通过 Nonetype 对象进行索引时会发生这种情况,例如,
>>> x = None
>>> x[0]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
x[0]
TypeError: 'NoneType' object is not subscriptable
为了正确解决您的问题,请发布完整代码或您设置的初始值self.pos
。您似乎将此变量设置为None
.
推荐阅读
- ios - 如何滚动到第一个单元格,同时在 CollectionView 上滑动最后一个单元格?
- ios - 将动画从一个视图控制器过渡到另一个视图控制器
- terminal - VS Code:如何在 linux 的集成终端中解开命令的输出
- javascript - 我如何限制功能 - (老虎机游戏)仅 5 圈 - 使用 do/while 循环
- javascript - 循环时“目标范围的坐标超出工作表的尺寸”错误
- docker - KEYSTORE.JKS 存在失败 - 以代码 1 #662 退出 - Confluent kafka
- java - 在 ELKI 中将 DBSCAN 与 RStarTree 一起使用时出现 UnsupportedOperationException
- c - 为什么 char [] 作为静态
- angular - Angular - 如何在任何时候触发列表的每个元素上的事件时不重建使用 *ngFor 创建的列表
- c# - 如何从 Assets 文件夹中获取所有预制件?在编辑器中获取无效的强制转换异常