我不明白为什么我的modulu运营商似乎收到了错误消息。虽然,我甚至不确定这个问题。这是我收到的错误消息:
Traceback (most recent call last):
File "c:\projectspython\myfirstchallenge.py", line 13, in <module>
check = (number) % (2)
TypeError: not all arguments converted during string formatting
代码:
number = input("Enter a number: ")
check = (number) % (2)
if check == 0:
print("your number is even.")
elif check >= 0:
print("Your number is odd.")
else:
print("something else")
答案 0 :(得分:3)
试试这个。您需要转换为int,或%
将尝试将其格式化为字符串。此外,如果没有给出数字,您应该确保您的程序不会出错。
while True:
try:
number = int(input("Enter a number: "))
break
except:
print("Enter a number")
check = number % 2
if check == 0:
print("your number is even.")
elif check >= 0:
print("Your number is odd.")
else:
print("something else")
答案 1 :(得分:1)
你只需要把它放在第一位。使用input
方法,它只需要一个字符串。所以在这里你只需要施展它。就是这样。
number = int(input("Enter a number: "))
check = (number) % (2)
print(check)
if check == 0:
print("your number is even.")
elif check >= 0:
print("Your number is odd.")
else:
print("something else")