gpt4 book ai didi

python - 为什么本应为 28 的结果却是 30?

转载 作者:行者123 更新时间:2023-12-01 05:02:10 26 4
gpt4 key购买 nike

我知道这是非常初级的,但我是编程新手,这让我发疯。

有人能告诉我为什么我得到的答案是“30”而不是“28”吗?

def days_in_month(month):
if month == 4 or 6 or 9 or 11:
return 30
else:
if month == 2:
return 28
else:
return 31

print(days_in_month(2))

最佳答案

以下内容不太正确(它在语法上是有效的,但没有执行您期望的操作):

if month == 4 or 6 or 9 or 11:

将其更改为:

if month in {4, 6, 9, 11}:

此外,您的顶部 if 语句有两个 else 子句。这不是有效的语法。我建议看看 tutorial了解 if 语句。

关于python - 为什么本应为 28 的结果却是 30?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25837574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com