Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以月为单位计算天数的python程序_Python - Fatal编程技术网

以月为单位计算天数的python程序

以月为单位计算天数的python程序,python,Python,我正试图编写一个python程序,让用户逐月输入数字,例如3月份,并输出天数,这是一个学校项目吗 def get_days(month): days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] return days[(month-1) % 12] 自从没有提及闰年以来,我就没有考虑过, 但如果你需要它: def get_days(month, leap=False): days = [31, 28, 31, 3

我正试图编写一个python程序,让用户逐月输入数字,例如3月份,并输出天数,这是一个学校项目吗

def get_days(month):
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    return days[(month-1) % 12]
自从没有提及闰年以来,我就没有考虑过,

但如果你需要它:

def get_days(month, leap=False):
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    return days[month-1] if not leap or month != 2 else 29

你的问题是什么?当这一年结束时,你会失败吗leap@rcvaram符合事实的为闰年添加了更多内容。通常情况下,它是有效的,我认为在第一个示例中使用模有什么意义?你能不能只做几天[第一个月]?如果你知道这是闰年,这会起作用,但这不仅仅是第%4年,只是供参考