编程序实现:输入任意一个年份、月份,输出该月份的公历日历(要求用循环控制打印)。
时间: 2024-02-13 21:06:00 浏览: 39
好的,我可以帮你写一个Python程序来实现这个功能。以下是代码:
```python
# 判断是否为闰年
def is_leap_year(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
# 输出日历
def print_calendar(year, month):
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_leap_year(year):
days_in_month[1] = 29
print("{0}年{1}月".format(year, month))
print("日 一 二 三 四 五 六")
# 计算该月第一天是星期几
first_day_of_week = 1 + sum([days_in_month[i] for i in range(month - 1)]) % 7
# 打印日历
for i in range(1, days_in_month[month - 1] + 1):
if i == 1:
print(" " * (first_day_of_week - 1), end="")
print("{0:2d} ".format(i), end="")
if (i + first_day_of_week - 1) % 7 == 0:
print()
if (days_in_month[month - 1] + first_day_of_week - 1) % 7 != 0:
print()
# 主程序
if __name__ == '__main__':
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
print_calendar(year, month)
```
你可以将这段代码保存为一个Python文件并运行。程序会要求你输入要查询的年份和月份,然后输出该月份的公历日历。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)