python - 判断数字是奇数还是偶数

标签 python

<分区>

我把它放进去,


def check(digit):
     if digit % 2 == 0:
         print("Even number")

check()

作为返回,我收到一条错误消息,

 Traceback (most recent call last):
 File "<pyshell#9>", line 1, in <module>
 check()
 TypeError: check() missing 1 required positional argument: 'digit'

谁能帮我弄清楚我做错了什么?

最佳答案

您没有使用任何参数调用您的函数 check。您还必须缩进您的 if 语句。

def check(digit):
     if digit % 2 == 0:
         print("Even number")
     else:
         print("Odd number")

check(3) # Prints "Odd number"
check(4) # Prints "Even number"

关于python - 判断数字是奇数还是偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22946970/

相关文章:

python - 解释一下这个算法

python - Numpy 是否给出了意想不到的结果?

python - 如何在Pythons Scikit-Learn中组合多种特征选择方法

python - 在决策树中查找到决策边界的距离

python - pandas DataFrame 添加一个新列和 fillna

python - 如何让 Python AWS Lambda 打开作为电子邮件对象存储在 S3 中的电子邮件

python - 从 python 脚本运行进程,进程永远不会停止

python - 关灯算法

python - 带有 Bokeh 面积图的分类轴

python - 使用 python-docx 在 MS word 中写入特定字体颜色的文本