[英]builtin function or method object is not iterable
I'm new to Python and stackoverflow too. 我也是Python和stackoverflow的新手。 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")
There are several problems in this code: 这段代码有几个问题:
string1 = str(input())
input
already returns a string, no need to call str
string1 = str(input())
input
已返回一个字符串,无需调用str
list1 = string1.split
You forgot ()
to actually call the split
method. list1 = string1.split
您忘记了()
实际调用split
方法。 if len(string1) == 10 or 20:
does not do what you think it does. if len(string1) == 10 or 20:
不执行您认为的操作。 It will always evaluate to True
since it is interpreted as (len(string1) == 10) or 20
. (len(string1) == 10) or 20
因此它将始终为True
。 You want len(string1) in (10, 20)
len(string1) in (10, 20)
num % 2 == 0
num
will be a string here, you want int(num) % 2 == 0
. num % 2 == 0
num
将是一个字符串,您需要int(num) % 2 == 0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.