gpt4 book ai didi

python - 类型错误 : 'float' object not iterable

转载 作者:IT老高 更新时间:2023-10-28 20:53:14 29 4
gpt4 key购买 nike

我在 Windows 7 上使用 python 3.2.2,我正在尝试创建一个接受 7 个数字的程序,然后告诉用户有多少是正数,有多少是负数,有多少是零。这是我到目前为止所得到的:

count=7
for i in count:
num = float(input("Type a number, any number:"))
if num == 0:
zero+=1
elif num > 0:
positive+=1
elif num < 0:
negative+=1

print (positive)
print (negative)
print (zero)

但是当我运行代码时,我得到了

TypeError: 'float' object is not iterable

如果我将第 3 行中的 float 替换为 int,我会遇到同样的问题,只是它表示“int”对象不可迭代。我也尝试将 count 的值从 7 更改为 7.0

现在我从一本 python 教程书中接受了这个挑战,但他们没有答案,据我所知,我已经按照他们提出的语法完成了所有工作。

The tutorial is here (PDF)

最佳答案

for i in count: 表示for i in 7:,这行不通。 in 之后的位应该是可迭代类型,而不是数字。试试这个:

for i in range(count):

关于python - 类型错误 : 'float' object not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8120019/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com