Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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,程序应提示用户输入月份和年份(作为整数),并显示月份的天数,显示月份名称。例如,如果用户输入了第2个月和2000年,程序应显示: Enter a month as an integer (1-12): 2 Enter a year: 2000 There are 29 days in February of 2000 def numberOfDays(月、年): daysInMonths=[31,28,31,30,31,30,31,31,30,31,30,31] 如果月>月(日/月)或年

程序应提示用户输入月份和年份(作为整数),并显示月份的天数,显示月份名称。例如,如果用户输入了第2个月和2000年,程序应显示:

Enter a month as an integer (1-12): 2
Enter a year: 2000
There are 29 days in February of 2000
def numberOfDays(月、年):
daysInMonths=[31,28,31,30,31,30,31,31,30,31,30,31]
如果月>月(日/月)或年<0或月<0:
return“请输入有效的月份/年份”
返回日期月[1月]+int((第%4年)=0,月==2)
def main():
年份=整数(输入(“输入年份:”)
month=int(输入(“以数字形式输入月份:”)
打印(月,年,“已”,numberOfDays(月,年),“天”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
在这个程序中,我想显示月份的名称,并在最后一页打印出来。 那么这个计划会是怎样的呢?我该怎么办? 例如,如果输入为1,则应将1指定为一月,并将其打印。

使用字典:

dict_month = {1:"January", 2: "February"} # and so on ...

print(dict_month.get(month), year, "has", numberOfDays(month, year) , "days")

您可以使用日历模块完成此操作:

import calendar
months = list(calendar.month_name) #gets the list of months by name
obj = calendar.Calendar()

y, m = int(input('Enter year: ')), int(input('Enter a month as an integer (1-12): '))
days = calendar.monthrange(y, m)[1]

print(f'There are {days} days in {months[m]} of {y}')
如果你想做的只是检查年份是否为闰年:

import calendar
print(calendar.isleap(int(input('Enter year: '))))

只是一张寻找新年的便条。它是
year%400==0或year%4==0和year%100!=0
问题出在哪里?“请看,”法汉,很高兴我能帮上忙!您能将此答案标记为所选答案吗?帮助将来的读者以与您相同的问题更快地找到答案,并给我几点建议:
import calendar
print(calendar.isleap(int(input('Enter year: '))))