所以我正在尝试编写一个代码,无论它是几号,它都会返回这个月的天数。这是我目前编写的代码。我有几个月份是对的,但其余的都不是。有人可以指出我在编码方面做错了什么吗?
def get_days_in_month (month):
if (month == 2):
return 28
elif (month == 4 + 6 + 9 + 11):
return 30
elif (month == 1 + 3 + 5 + 7 + 8 + 10 +12):
return 31
else:
return 31
最佳答案
更好的主意:
使用 python 的内置计算器。使用monthrange并传入(int)年月
monthrange(year,month): Returns weekday of first day of the month and number of days in month, for the specified year and month
from calendar import monthrange
def get_days_in_month (year,month):
month_data= monthrange(year, month)
# If you only want DAYS, use month_data[1]
get_days_in_month(2018,1)
关于python - 几个月写几天的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53196554/