【问题标题】:Python 3.3.3: TypeError: list indices must be integers, not floatPython 3.3.3:TypeError:列表索引必须是整数,而不是浮点数
【发布时间】:2014-02-06 16:58:36
【问题描述】:

我有一个 python 2.7.6 程序,我想转换为 3.3.3。

我收到一个错误:

File "H:\My Game Python 3,3\MoonSurvival.py", line 199, in run
  self.moon.levelStructure[x][y] = None

回溯是:

Traceback (most recent call last):
  File "H:\My Game Python 3,3\MoonSurvival.py", line 591, in <module>
    Game().run()
  File "H:\My Game Python 3,3\MoonSurvival.py", line 199, in run
    self.moon.levelStructure[x][y] = None
TypeError: list indices must be integers, not float

但是当我查看levelStructure 的代码时,并没有错。这是分配 levelStructure 的代码:

tempBlock = Block(x*64, y*64)
# add block to both and self.levelStructure
self.levelStructure[x][y] = tempBlock

如您所见,levelStructure 是 tempBlock。当它成倍增加时,它似乎在浮动。我知道除法是两个斜线/。我试图乘以 '*' 但我仍然得到错误。我对 3.3.3 语法并不完全熟悉,因为我通常使用 2.7.6。

块代码:

class Block(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('data/images/moon.jpg')

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def render(self, surface):
        surface.blit(self.image, self.rect)

是什么导致了这个错误?

【问题讨论】:

  • Block 是什么,我们能看到它的代码吗?
  • 如果 xy 在乘法之前是浮点数,那么它们在乘法之后将保持浮点数。乘以一个整数不会改变这一点。

标签: python python-2.7 python-3.x pygame


【解决方案1】:

由于您是从 Python 2.7 -> Python 3.x 转换的,因此几乎肯定是楼层划分的问题。

我不确定您在哪里生成 xy,但是如果您在任何时候将这些数字除以任何值,请确保在 Python 3 中使用 // 运算符(如果您期望的话)一个整数结果。

在 Python 2 中,5/2 == 2 必须执行 5/2.0 才能获得浮点结果。在 Python 3 中,5/2 == 2.5 并且您必须执行 5//2 才能获得除法的下限整数结果。很有可能,每当您在 xy 上进行操作时,您都会将其除以 Python 2 中给您一个整数但在 Python 3 中给您留下的浮点数,所以当您尝试使用它作为列表索引...BOOM

【讨论】:

  • 我一直在使用 // 来获取一个整数,因为我必须这样做来修复我的碰撞。这是唯一需要 // 的地方,因为没有使用其他分区。
  • 不知道为什么你的 xy 不是整数。在你的self.levelStructure[x][y] = tempBlock 行之前,你为什么不做print("x is {} and its type is {}".format(x,type(x))) 并重复y,然后告诉我结果。
  • 好的,现在试试。刚刚尝试过,根本没有打印任何内容:/
  • @GhostFrag1 如果没有打印任何内容并且您将这些行放在调用self.levelStructure[x][y] 之前,那么这不是您要抛出异常的行。实际上,在堆栈跟踪中,它看起来像第 199 行,self.moon.levelStructure[x][y]。尝试在此之前放置相同的打印语句。
  • 确实会有。要做一些艰苦的寻找。将检查混合中的任何 / 。我现在觉得好傻。。我看了看,字面上就在印刷品的正上方有一些 / ......
【解决方案2】:

您将浮点数作为数组的索引传递,该数组仅接受整数等整数。

如何在 Python 3.3.3 中尽可能简单地重现此错误:

>>> stuffi = []
>>> stuffi.append("foobar")
>>> print(stuffi[0])
foobar
>>> stuffi[0] = "failwhale"
>>> print(stuffi[0])
failwhale
>>> stuffi[0.99857] = "skipper"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not float

尝试将整数类型转换添加到变量中,这样:

self.levelStructure[x][y] = tempBlock

变成:

self.levelStructure[int(x])[int(y)] = tempBlock

如果可行,您可以将其添加到:

tempBlock = Block(x*64, y*64)

所以它看起来像:

tempBlock = Block(int(x*64), int(y*64))

【讨论】:

  • 肯定会尝试一下看起来这可能会有所帮助,因为它肯定被告知是一个 int。试过了,似乎没有什么不同。游戏无法运行,并且发生了更多错误。
  • 这可能最终会起作用,但可能不是“正确”的答案。
  • 在将浮点数用作数组的索引之前将其转换为 int 是一种“管道胶带程序员式的扫除地毯下的问题”的解决方案。最好从源头解决错误,并确保浮点数不会从生成数组索引的计算中出来。如果舞厅地板上到处都是弹珠,解决办法是清除弹珠,而不是让每个人都穿大靴子。
猜你喜欢
  • 2012-11-01
  • 2019-03-03
  • 2020-01-15
  • 2021-07-19
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多