输入年月打印日历python
时间: 2024-01-27 12:49:40 浏览: 35
可以使用Python的calendar模块来实现。以下是一个简单的示例:
```python
import calendar
# 输入年份和月份
year = int(input("请输入年份: "))
month = int(input("请输入月份: "))
# 打印日历
print(calendar.month(year, month))
```
运行程序后,会提示你输入年份和月份,然后会打印出相应的日历。例如,输入年份为2021,月份为10,输出结果如下:
```
请输入年份: 2021
请输入月份: 10
October 2021
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
```
相关问题
python打印当月日历的编程代码
以下是Python实现打印当月日历的代码:
```python
import calendar
# 获取当前年月
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
# 打印当月日历
calendar.prmonth(year, month)
```
执行代码后,程序会要求用户输入年份和月份,然后调用`calendar.prmonth()`函数打印指定年月的日历。输出结果示例:
```
请输入年份:2022
请输入月份:9
September 2022
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
```
其中,`calendar.prmonth(year, month)`函数会打印指定年月的日历,其中`year`和`month`分别表示年和月,输出结果以周日为一周的第一天。
用python编写一个程序 输入年和月,生成输入年月的日历表。
可以使用Python中的calendar模块来生成日历表。以下是一个简单的程序,可以根据用户输入的年份和月份生成对应的日历表。
```python
import calendar
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
# 使用calendar模块的monthrange函数获取指定月份的天数
days = calendar.monthrange(year, month)[1]
# 使用calendar模块的month函数生成指定月份的日历表
cal = calendar.month(year, month)
print("以下是%d年%d月的日历表:" % (year, month))
print(cal)
```
运行程序后,用户需要输入要查询的年份和月份,程序会根据用户输入的年份和月份生成对应月份的日历表。例如,输入年份为2021,月份为5,程序会输出以下结果:
```
请输入年份:2021
请输入月份:5
以下是2021年5月的日历表:
May 2021
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
```