在本節中,我們將通過一個例子展示如何編寫一個 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))
在這個 Python 日曆示例程序中,首先,我們要導入 Python 編程語言提供的日曆庫。該庫中的一組函數可以幫助您在日曆上執行許多操作。
import calendar
接下來,我們要求用戶使用以下語句輸入年和月的數字。為了確保他們輸入的是整數值,我們在這裡使用了 typecast。
year = int(input("Please Enter the year Number: "))
month = int(input("Please Enter the month Number: "))
Python 日曆示例程序中的以下語句顯示了月曆(這裡是 2016 年 2 月)。
print(calendar.month(year, month))
從上面的語句中,您可以觀察到我們正在調用 python 日曆庫中的 month 函數。這個 Python 函數接受兩個參數(即年和月)。
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/130265.html