2

此代码给出错误

print('type a whole number:')
n = input()
if n % 2 == 1:
    print('Odd');
else:
    print('Even');

我假设我必须对 if 语句中的变量 n 做一些特别的事情?我是 Python 的初学者。

4

3 回答 3

4

以下是解决方法:

n = int(input("type a whole number:"))

由于input()返回一个字符串,因此您需要先使用int()将其转换为 int 。

于 2013-03-19T10:05:17.027 回答
3

您需要先转换n为整数,在 py 3.x 中input()返回一个字符串。:

n = int(input())
于 2013-03-19T10:04:49.237 回答
0

首先将用户输入转换n为整数。
即简单地改变:

n = input()

到 :

n = int(input())

此外,input()可以将字符串作为参数,在输入之前打印。
所以,你可以改变

print('type a whole number:')
n = int(input())

n = int(input('type a whole number:'))
于 2013-03-19T10:12:58.383 回答