Python之函数与模块化程序设计(打印输出月历)
时间: 2024-01-18 08:02:28 浏览: 69
好的,我很乐意回答你关于 Python 函数与模块化程序设计的问题,下面是一个简单的打印输出月历的示例程序。
首先,我们需要导入 Python 中的 time 和 calendar 模块:
```python
import time
import calendar
```
然后,我们定义一个函数 print_calendar(year, month),用于打印指定年月的月历。函数中调用了 calendar.month() 方法来获取指定月份的日历,并使用 time.strftime() 方法获取当前时间,并打印出当前年月的标题。
以下是完整的程序代码:
```python
import time
import calendar
def print_calendar(year, month):
# 获取当前时间
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 打印当前年月
print("当前时间为:", now)
print("打印%d年%d月的月历:" % (year, month))
# 打印指定年月的月历
cal = calendar.month(year, month)
print(cal)
# 测试
print_calendar(2022, 11)
```
运行程序后,输出结果如下:
```
当前时间为: 2022-11-15 14:53:29
打印2022年11月的月历:
November 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
```
这就是一个简单的 Python 函数与模块化程序设计的示例,希望对你有所帮助!