英文:
"TypeError: 'int' object is not iterable" never shuts up
问题
我正在尝试制作一个视频游戏,但程序一直报错。你知道我做错了什么吗?
以下是我的代码:
# 不断报错的代码部分
for elem in len(enemyList):
# 我一直尝试加入global,但从未成功
enemyList = []
...
def wave_call():
if wave == 1:
global enemyList
enemyX = []
enemyY = []
for elem in range(len(enemyX)):
enemyX.append(random.randint(0, 1500))
enemyY.append(random.randint(0,750))
enemyList.append("")
screen.blit(enemy, (enemyX[elem], enemyY[elem]))
英文:
I am trying to make a video game, but the program keeps shooting errors. Any idea what I'm doing wrong?
My code below:
#code that keeps shooting error
for elem in(len(enemyList)):
#I keep putting global in here, but it never works
enemyList = []
...
def wave_call():
if wave == 1:
global enemyList
enemyX = []
enemyY = []
for elem in range(len(enemyX)):
enemyX.append(random.randint(0, 1500))
enemyY.append(random.randint(0,750))
enemyList.append("")
screen.blit(enemy, (enemyX[elem], enemyY[elem]))
答案1
得分: 3
On your first line: for elem in(len(enemyList)):
You forgot the range, it should be for elem in range(len(enemyList)):
英文:
On your first line : for elem in(len(enemyList)):
. You forgot the range, it should be for elem in range(len(enemyList)):
答案2
得分: 0
以下是翻译好的内容:
代码中出现错误的原因是在 for elem in(len(enemyList))
部分,因为你不能直接迭代整数值(因为你要求程序迭代长度)。
为了修复这个问题,尝试使用 range()
函数来进行迭代,如下所示:
for elem in range(len(enemyList)):
这段代码将循环遍历敌人列表,并允许你使用 elem
来访问敌人。
英文:
The reason why the code for elem in(len(enemyList)): is causing an error is that you cannot iterate directly over an integer value (since you ask the program to iterate over a length).
in order to fix this, try using the range() function to iterate over it as follow:
for elem in range(len(enemyList))
this code will loop over the enemy list and allows you to use elem to acess the enemy.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论