我目前正在阅读“How to think like a computer scientist: Learning with Python”(绿茶出版社,2002 年 1 月。)
我无法使任何定义函数起作用。我已经完全按照书中的内容复制了它,但它不起作用。我做错了什么?
代码如下:
def printParity(x):
if (x)%2 == 0:
print (x), ("is even")
else:
print (x), ("is odd")
它只是打印 (x) 输入而不是 (x) 是奇数或偶数。
最佳答案
你想要:
def printParity(x):
if x % 2 == 0:
print(x, "is even")
else:
print(x, "is odd")
你的声明print (x), ("is even")
实际上是在创建一个元组,可以在控制台看到:
>>> x=2
>>> print (x), ("is even")
2
(None, 'is even')
关于python - Python 3.2.2 中的用户定义函数 (def),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12201337/