如何解决TypeError:不是在格式化字符串时转换了所有参数

问题描述 投票:0回答:1

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

代码:

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)*
python python-3.7
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.