【发布时间】:2021-12-11 02:46:03
【问题描述】:
我正在尝试输出日历上的日期,例如:2021-02-02 2021-02-03 2021-02-04 2021-02-05 等。 我从https://www.tutorialbrain.com/python-calendar/ 复制了这段代码,所以我不明白为什么会出现错误。
import calendar
year = 2021
month = 2
cal_obj = calendar.Calendar(firstweekday=1)
dates = cal_obj.itermonthdays(year, month)
for i in dates:
i = str(i)
if i[6] == "2":
print(i, end="")
错误:
if i[6] == "2":
IndexError: string index out of range
Process finished with exit code 1
【问题讨论】:
-
if不是多余的,因为您已经将月份设置为 2?
标签: python string function calendar index-error