确定奇数或偶数用户输入

时间:2014-09-06 15:35:53

标签: python

我试图通过模运算符确定输入;但是,当我运行此代码时,我得到一个TypeError

    number = input(enter a number)
    if(number%2 == 0):
        print( 'obviously',number, 'is even') #conditional that prints the number and a statment 

    else:
        print( 'obviously', number, 'is odd');

我的问题是:导致TypeError

的原因

1 个答案:

答案 0 :(得分:2)

在Python 3中,input返回一个字符串。您不能对字符串执行模数。所以你需要把它变成一个整数。

number = int(input('Enter a number:'))