TypeError:'type'对象在for循环中不可迭代

问题描述 投票:-1回答:1

我看过其他线程,无法弄清楚出了什么问题。所有的答案都太复杂了,我无法很好地处理。

我不断收到以下错误:

TypeError:“类型”对象不可迭代

而且我不知道为什么。

以下是代码段:

calcWhat = 4
elif o == "^":
    pwr = int(input("To the power of what? "))
    pwr = pwr - 1 #So that if it's one then it doesnt multiply at all
    ans = calcWhat * calcWhat
    for pwr in range:
        ans = ans + (calcWhat * calcWhat)

我认为这是正确的代码,但是如果不解决此错误,我什至看不到!

怎么了?

python python-3.x exception typeerror
1个回答
0
投票

似乎您正在尝试遍历range,它是type(如错误所提及)。您可能要调用函数range()并为其指定范围限制。您的情况是:

for _ in range(pwr):
© www.soinside.com 2019 - 2024. All rights reserved.