我收到模数运算符的错误

时间:2018-03-30 22:36:14

标签: python python-3.x

我不明白为什么我的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")

2 个答案:

答案 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")