Dhanush M
Dhanush M

Reputation: 3

builtin function or method object is not iterable

I'm new to Python and stackoverflow too. I'm trying to write a program that can differentiate between odd nums and even nums but I'm getting this err. Help!. This is my prog:

print("Enter the 10 numbers separated by space to distinguish : ")
string1 = str(input())
if len(string1) == 10 or 20:
    list1 = string1.split
    for num in list1:
        #check for odd
        if num % 2 == 0 :
            print(num)
        else:
            print(f'Odd number : {num}')
else:
    print("Please enter 10 numbers")

Upvotes: 0

Views: 211

Answers (1)

DeepSpace
DeepSpace

Reputation: 81614

There are several problems in this code:

  • string1 = str(input()) input already returns a string, no need to call str
  • list1 = string1.split You forgot () to actually call the split method.
  • if len(string1) == 10 or 20: does not do what you think it does. It will always evaluate to True since it is interpreted as (len(string1) == 10) or 20. You want len(string1) in (10, 20)
  • num % 2 == 0 num will be a string here, you want int(num) % 2 == 0.

Upvotes: 2

Related Questions