从零开始学Python

Python习题册012:打印给定年月的日历

2019-01-15  本文已影响13人  iLester

任务011描述

用Python编写一个程序,输入指定的月份和年度,打印出该月份的日历。
说明:可以使用calendar模块。

分析及示例

Python calendar.month(theyear, themonth, w=0, l=0):
Python的calendar.month(theyear, themonth, w=0, l=0)方法可以返回TextCalendar类的formatmonth()执行结果,返回值以多行文本的形式显示当月的日历。
l(L的小写)可以用于指定每一周占据的行数。

示例代码:

import calendar
y = int(input('Input the year:'))
m = int(input('Input the month:'))
print(calendar.month(y, m))

运行结果:

Input the year:2019
Input the month:1
    January 2019
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
上一篇 下一篇

猜你喜欢

热点阅读