python万年历代码
时间: 2023-10-14 13:28:34 浏览: 73
好的,以下是Python实现的万年历代码(不含输入验证):
```python
# 导入模块
import datetime
# 获取当前日期
now = datetime.datetime.now()
# 获取本年
this_year = now.year
# 获取本月
this_month = now.month
# 获取本日
this_day = now.day
# 获取本月第一天是星期几
first_day_of_month = datetime.date(this_year, this_month, 1).weekday()
# 计算当月天数
if this_month == 12:
days_of_month = (datetime.date(this_year+1, 1, 1) - datetime.date(this_year, this_month, 1)).days
else:
days_of_month = (datetime.date(this_year, this_month+1, 1) - datetime.date(this_year, this_month, 1)).days
# 输出日历标题
print(f" {this_year}年{this_month}月")
print("日 一 二 三 四 五 六")
# 输出日历内容
for i in range(1-first_day_of_month, days_of_month+1):
if (i > 0) and (i % 7 == (8 - first_day_of_month) % 7):
print()
if i == this_day:
print(f" \033[7m{i:2}\033[0m", end="")
else:
print(f" {i:2}", end="")
print()
```
希望对您有所帮助!