共計 375 個字符,預計需要花費 1 分鐘才能閱讀完成。
可以使用 calendar 模塊中的 monthrange()方法來實現輸入年月后輸出天數。
下面是一個示例代碼:
import calendar
year = int(input(" 請輸入年份: "))
month = int(input(" 請輸入月份: "))
# 使用 monthrange()方法獲取指定年月的第一天是星期幾和該月的天數
first_day, num_days = calendar.monthrange(year, month)
print(f"{year}年 {month} 月的天數為: {num_days}天 ")
該代碼首先使用 input()函數獲取用戶輸入的年份和月份,然后使用 calendar.monthrange()方法獲取指定年月的第一天是星期幾和該月的天數。最后使用 print()函數輸出結果。
注意:輸入的年份和月份需要為整數。
丸趣 TV 網 – 提供最優質的資源集合!
正文完