“TypeError: ‘int’ object is not iterable” 永远不会停止

huangapple go评论124阅读模式
英文:

"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.

huangapple
  • 本文由 发表于 2023年4月1日 01:13:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75901101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定