Throw typeerror: 'float' object is not iterable in Python

Dung Do Tien Oct 19 2021 47

Hello you guys, I a newbie in Python and I'm also studying more about Python.

I want to get some number total from user input then loop it and find total even and odd number. I have some line of code as below:

 even = 0
odd = 0
count = float(input("Enter total point will get: "))
for i in count:
    num = float(input("Type a number:"))
    if num % 2 == 0:
        even +=1  
    else:
        odd +=1
        
print("even: ", even)
print("odd: ",odd)

But I get an exception throw TypeError: 'float' object is not iterable when I run code above.

 Enter total point will get: 3
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    for i in count:
TypeError: 'float' object is not iterable

And I am using python 3.x

Anyone can explain it to me? 

How can I solve it?

Thanks for any suggestions.

Have 1 answer(s) found.
  • A

    Ana Maria Oct 19 2021

    I think you need an integer not a float number. Follow solution below:

    Step 1: Change float() to int() function.

    Change

     count = float(input("Enter total point will get: "))

    to

     count = int(input("Enter total point will get: "))

    Step 2: Use range() function to hepl loop range number.

    Change

     for i in count:

    to

     for i in range(count):

    I hope it helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close