Search code examples
pythonpython-3.xfor-loopfloating-pointtypeerror

TypeError: 'float' object not iterable


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. How do I resolve this error?


Solution

  • 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):