简体   繁体   English

如何修复 TypeError:并非所有参数都在字符串格式化期间转换

[英]How to fix TypeError: not all arguments converted during string formatting

I have a problem with my program that is supposed to sort even and odd numbers.我的程序有问题,应该对偶数和奇数进行排序。

The code:编码:

everything = []
print("Type all your numbers and then type stop")
while True:
    nove = input()
    if nove == "stop":  
        break
    else:
        everything.append(nove)
numbers = [s for s in everything if s.isdigit()]
print("All numbers:")
for number in numbers:
    print(number)

print("Odd numbers:")
for number in numbers:
    if number % 2 == 1:
        print(number)
    else:
        continue
print("Even numbers:")
for number in numbers:
    if number % 2 == 0:
        print(number)
    else:
        continue

It returns this:它返回这个:

*Type all your numbers and then type stop
>68
>11
>stop  All numbers: 68 11 Odd numbers: Traceback (most recent call last):    File "tridicka.py", line 16, in <module>
     if number % 2 == 1: TypeError: not all arguments converted during string formatting
     ------------------ (program exited with code: 1)*

Your inputs are string, you need to parse thel as int您的输入是字符串,您需要将 l 解析为int

numbers = [int(s) for s in everything if s.isdigit()]

Also this part is useless, as there is no other cde to run after这部分也没有用,因为没有其他 cde 可以运行

else:
    continue

Just do做就是了

print("Odd numbers:")
for number in numbers:
    if number % 2 == 1:
        print(number)

print("Even numbers:")
for number in numbers:
    if number % 2 == 0:
        print(number)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 Python 中的“TypeError:并非所有参数都在字符串格式化期间转换” - How to fix 'TypeError: not all arguments converted during string formatting' in Python TypeError 并非在字符串格式化期间转换的所有参数 - TypeError Not all Arguments Converted During String Formatting 类型错误:字符串格式化期间并非所有 arguments 都转换 - TypeError : not all arguments converted during string formatting TypeError:在字符串格式化期间并非所有参数都已转换 - TypeError: not all arguments converted during string formatting 类型错误:并非所有参数都在字符串格式化期间转换 - TypeError: not all arguments converted during string formatting TypeError:在字符串格式化期间并非所有参数都已转换 - TypeError: not all arguments converted during string formatting 类型错误:并非所有参数都在字符串格式化期间转换。 有关如何解决此问题的任何帮助? - TypeError: not all arguments converted during string formatting. Any help on how to fix this? 如何修复并非所有在字符串格式化期间转换的参数? - How can I fix not all arguments converted during string formatting? psycopg2 typeerror:TypeError:字符串格式化期间并非所有 arguments 都转换了 - psycopg2 typeerror: TypeError: not all arguments converted during string formatting DURING MODULES-typeError:在字符串格式化期间并非所有参数都已转换 - DURING MODULES - typeError: not all arguments converted during string formatting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM