Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'float' object not iterable

Tags:

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)

like image 519
hamsolo474 - Reinstate Monica Avatar asked Nov 14 '11 10:11

hamsolo474 - Reinstate Monica


People also ask

Why is float object not iterable?

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.

How do you fix TypeError argument of type float is not iterable?

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 do you fix an object not iterable?

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.

How do I iterate through a float list in Python?

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.


2 Answers

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): 
like image 131
Thomas K Avatar answered Sep 22 '22 06:09

Thomas K


use

range(count)

int and float are not iterable

like image 41
Mohammad Efazati Avatar answered Sep 18 '22 06:09

Mohammad Efazati