24
votes

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)

2
for i in count is not in the tutorial you've attached. - cwallenpoole
Please do print(...) rather than print (...). In Python 3 print is a function, so treat it just like any other function in how you format it. (PEP 8 would also like you to change a=b and a+=b to a = b and a += b.) - Chris Morgan
You also need to initialize the three variables that you're trying to print: i.e. put zero = positive = negative = 0 before the loop. - ekhumoro
@cwallenpoole i might not have phrased my question correctly, i trying to complete question 13 in section 3.3 (page 35 in that pdf) and i thought i had used the correct syntax (which i had learnt from other pages on that pdf) but as Chris Morgan pointed out i was missing the range function ChrisMorgan i will begin removing spaces immediately, thank you again for your help ekhumoro thanks ill do that now - hamsolo474 - Reinstate Monica

2 Answers

30
votes

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):
3
votes

use

range(count)

int and float are not iterable