简体   繁体   English

内置函数或方法对象不可迭代

[英]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.

相关问题 'builtin_function_or_method' object 不可迭代 - 'builtin_function_or_method' object is not iterable 使用 os.walk 不可迭代 builtin_function_or_method' 对象 - builtin_function_or_method' object is not iterable using os.walk 显示TypeError:'builtin_function_or_method'对象在Odoo中不可迭代 - showing TypeError: 'builtin_function_or_method' object is not iterable in Odoo Python'builtin_function_or_method'对象不是可迭代错误 - Python 'builtin_function_or_method' object is not iterable error python搜索中的'builtin_function_or_method'对象不可迭代'错误? - 'builtin_function_or_method' object is not iterable' error in python search? 使用max()时获取“'builtin_function_or_method'对象不可迭代” - Getting “'builtin_function_or_method' object is not iterable” when using max() 'builtin_function_or_method' object 不可迭代,带有列表的 for 循环 - 'builtin_function_or_method' object is not iterable, for loop with list 发生异常:TypeError 'builtin_function_or_method' object is not iterable - Exception has occurred: TypeError 'builtin_function_or_method' object is not iterable python “类型错误:‘builtin_function_or_method’ object 不可迭代” - python "TypeError: 'builtin_function_or_method' object is not iterable" 主循环“ builtin_function_or_method”对象不可迭代 - main loop 'builtin_function_or_method' object is not iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM