问题
This code gives an error
print('type a whole number:')
n = input()
if n % 2 == 1:
print('Odd');
else:
print('Even');
I'm assuming there's something special I have to do to variable n in the if statement? I am a beginner to Python.
回答1:
Here is how to fix it:
n = int(input("type a whole number:"))
Since input() returns a string, you need to convert it to an int first, using int().
回答2:
You need to convert n to an integer first, in py 3.x input() returns a string.:
n = int(input())
回答3:
Convert the user input n to an integer first.
i.e. Simply Change :
n = input()
To :
n = int(input())
Also, input() can take a string as an argument, which is printed before taking the input.
So, you can change
print('type a whole number:')
n = int(input())
To
n = int(input('type a whole number:'))
来源:https://stackoverflow.com/questions/15496408/python-not-all-arguments-converted-during-string-formatting