python - Python 3.2.2 中的用户定义函数 (def)

标签 python python-3.x function

我目前正在阅读“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/

相关文章:

python - Python PyMT脚本在打开时崩溃

python - 使用反斜杠将字符串列表转换为正确的目录名称

python-3.x - 这是将 Keras 与 tensorflow 数据集一起使用时的错误吗?

python - 找到字典键的交集并将它们组合起来

C 函数中的 Const 声明和实现

python - 执行线程 5 by 5

python - Django 2、python 3.4 无法解码 urlsafe_base64_decode(uidb64)

python - 无法弄清楚查询失败的原因

python - 导入使用 PyInstaller 点引用的本地模块

javascript - 如何将无限数量的参数传递给 console.log 调用?