TypeError:'float'对象不可迭代 [英] TypeError: 'float' object not iterable

查看:973
本文介绍了TypeError:'float'对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  count = 7 
for i in count:
num = float(input(输入一个数字,任意数字:))
if num == 0:
zero + = 1
elif num> 0:
正值+ = 1
elif num< 0:
负数+ = 1

打印(正数)
打印(负数)
打印(零)
pre>

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

  TypeError: 'float'对象是不可迭代的

如果我用int替换第3行的float,除了说'int'对象是不可迭代的。我也尝试将计数值从7改为7.0现在我从一个Python教程书中接受了这个挑战,他们没有答案,我可以告诉我已经做了他们提出的语法中的一切。



教程在这里(PDF)

在计数:意味着为我在7:,这将无法正常工作。 中之后的位应该是可迭代的类型,而不是数字。试试看:

 对于范围(count)中的我:


I'm using python 3.2.2 on windows 7 and I'm trying to create a program which accepts 7 numbers and then tells the user how many are positive, how many are negative and how many are zero. this is what I have got so far:

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)

But when I run the code I get

TypeError: 'float' object is not iterable

If I replace float in line 3 with int I get the same problem except it says that the 'int' object is not iterable. I have also tried changing the value of count from 7 to 7.0

Now I took this challenge from a python tutorial book and they don't have the answer, and from what I can tell I have done everything within the syntax they put forward.

The tutorial is here (PDF)

解决方案

for i in count: means for i in 7:, which won't work. The bit after the in should be of an iterable type, not a number. Try this:

for i in range(count):

这篇关于TypeError:'float'对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆