python输入年月输出天数
作者:野牛程序员:2023-07-05 20:16:02python阅读 3016
以下是一个使用Python编写的程序,根据输入的年份和月份输出该月的天数:
import calendar
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
days = calendar.monthrange(year, month)[1]
print(f"{year}年{month}月有{days}天。")这个程序中使用了Python的内置模块calendar,其中的monthrange()函数返回一个元组,包含指定年份和月份的第一天是星期几以及该月的天数。我们从返回的元组中取出第二个元素,即该月的天数,并打印输出。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:python输入月份判断天数
- 下一篇:python年份判断二月天数
