The problem is that you take user input:
x = input()
Now x
is a str
. So, on this line:
elif x % 2 == 0: #even
The %
operator acts as a string interpolation operator.
>>> mystring = "Here goes a string: %s and here an int: %d" % ('FOO', 88)
>>> print(mystring)
Here goes a string: FOO and here an int: 88
>>>
However, the input
you gave does not have a format specifier, thus:
>>> "a string with no format specifier..." % 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
You need to convert your user input into an int
for the %
operator to perform the modulo operation.
x = int(input())
Now, it will do what you want:
>>> x = int(input("Gimme an int! "))
Gimme an int! 88
>>> x % 10
8
>>>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…