0

我已经搜索了好几次,我无法弄清楚这是什么问题。相关的代码行是:

WallList=[]
def createWallList():
    i=0
    while i<=numberWalls:
        newWall=box(pos=(0,(arenaSize-(i*2))-1,0), height=.1, width=1, length=(randomValue(0,20)), color=color.green)
        WallList.append(newWall)
        i=i+1

if WallList[i].pos.y>arenaSize:
    WallList[i].pos.y=-arenaSize

我在控制台中收到错误消息:

Traceback (most recent call last):
    File "/Users/samwhaverly/Documents/FallDownGameWIP.py", line 76
    if WallList[i].pos.y>arenaSize:
IndexError: list index out of range

对不起,这里有这么多,但我不知道出了什么问题。如果你们中的任何人都可以提供见解,那将非常有帮助。如有必要,我可以提供更多详细信息。

4

1 回答 1

1

You're getting an error because your indentation is wrong. Here's what's happening:

i = 0
Is i <= numberWalls? Yes.
Make a new wall.
Increment i to 1.
Is i <= numberWalls? Yes.
Make a new wall.
Increment i to 2.
...
Increment i to numberWalls + 1.
Is i <= numberWalls? No.
if WallList[i] <- ERROR: there is no wallList[i]
于 2012-12-03T23:39:01.097 回答