Python: not all arguments converted during string formatting

岁酱吖の 提交于 2019-12-24 01:43:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!