Python 日历示例

在本节中,我们将通过一个示例展示如何编写一个 Python 程序来显示月历。

Python 日历示例

此示例接受年份和月份作为输入变量。通过使用这些值,此示例程序将显示月历。

import calendar

# ask of month and year
year = int(input("Please Enter the year Number: "))
month = int(input("Please Enter the month Number: "))

print(calendar.month(year, month))
Calendar Example

在此示例程序中,首先,我们导入编程语言提供的日历库。该库中的函数组可以帮助您执行许多日历操作。

import calendar

接下来,我们使用以下语句要求用户输入年份和月份。为了确保他们输入的是整数值,我们在此使用了类型转换。

year = int(input("Please Enter the year Number: "))
month = int(input("Please Enter the month Number: "))

示例代码中的以下语句显示了月历(此处为 2016 年 2 月)。

print(calendar.month(year, month))

从上面的语句中,您可以看到我们调用了日历库中存在的 month 函数。这个 Python 函数接受两个参数(即年份和月份)。