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)
Floating-point numbers do not store multiple values like a list or a dictionary. If you try to iterate over a float , you will raise the error “TypeError: 'float' object is not iterable” because float does not support iteration. You will get a similar error if you try to iterate over an integer or a NoneType object.
The Python "TypeError: argument of type 'float' is not iterable" occurs when we use the membership test operators (in and not in) with a float value. To solve the error, correct the assignment or convert the float to a string, e.g. str(my_float) .
How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.
Use the for Loop to Convert All Items in a List to Float in Python. We can use the for loop to iterate through the list and convert each element to float type using the float() function. We can then add each element to a new list using the append() function.
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):
use
range(count)
int and float are not iterable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With