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

时间:2020-03-20 11:26:04

标签: python python-3.7

我的程序出现问题,应该对偶数和奇数进行排序。

代码:

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

它将返回以下内容:

*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)*

1 个答案:

答案 0 :(得分:0)

您的输入是字符串,您需要将其解析为int

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

这部分也没有用,因为之后没有其他cde要运行

else:
    continue

只需

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)