Reputation: 695
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?
Upvotes: 30
Views: 289635
Reputation: 23081
In short, check if you have unexpected/"hidden" NaN values in the data passed to a function that expects an iterable such as string, list etc.
As the other answers pointed out, this error occurs when your code expects an iterable but a float is passed to it. Missing data is often represented as NaN (which is a float), so if pass float or a NaN to a function that expects an iterable, it throws this error. Apart from the explicit loop (for i in 7.0
) in the OP, many of the built-in functions such as list()
, set()
, tuple()
, dict()
, enumerate()
, all()
, any()
, max()
, min()
, sum()
etc. expect an iterable, so if you pass a float to them, e.g. max(float('nan'))
or list(1.5)
, you'll get the error in the title.
Upvotes: 0
Reputation: 40340
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):
Upvotes: 36