我認爲你的縮進是搞砸了。否則,它看起來對我來說,你的代碼工作就好了... ...
>>> from random import randint
>>> for x in range(7): # test all possible values
... enemy_spaceship_index = x
... appearancesLeft = []
... for i in range(7):
... if i == enemy_spaceship_index:
... appearancesLeft.append(3)
... elif i != enemy_spaceship_index:
... appearancesLeft.append(2)
... print appearancesLeft
...
[3]
[3, 2]
[3, 2, 2]
[3, 2, 2, 2]
[3, 2, 2, 2, 2]
[3, 2, 2, 2, 2, 2]
[3, 2, 2, 2, 2, 2, 2]
[2]
[2, 3]
[2, 3, 2]
[2, 3, 2, 2]
[2, 3, 2, 2, 2]
[2, 3, 2, 2, 2, 2]
[2, 3, 2, 2, 2, 2, 2]
[2]
[2, 2]
[2, 2, 3]
[2, 2, 3, 2]
[2, 2, 3, 2, 2]
[2, 2, 3, 2, 2, 2]
[2, 2, 3, 2, 2, 2, 2]
[2]
[2, 2]
[2, 2, 2]
[2, 2, 2, 3]
[2, 2, 2, 3, 2]
[2, 2, 2, 3, 2, 2]
[2, 2, 2, 3, 2, 2, 2]
[2]
[2, 2]
[2, 2, 2]
[2, 2, 2, 2]
[2, 2, 2, 2, 3]
[2, 2, 2, 2, 3, 2]
[2, 2, 2, 2, 3, 2, 2]
[2]
[2, 2]
[2, 2, 2]
[2, 2, 2, 2]
[2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 3]
[2, 2, 2, 2, 2, 3, 2]
[2]
[2, 2]
[2, 2, 2]
[2, 2, 2, 2]
[2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 2, 3]
您在「appearancesLeft = []」之前有一個缺失的換行符「 –