Python 3.x 如何修复';缩进错误:应为缩进块';用python
我发现缩进错误 缩进错误:在审核日第33行应出现缩进块= 周2[“日历.星期一”] 这是代码,错误在最后一行,我使用的是最新的python3.7版本Python 3.x 如何修复';缩进错误:应为缩进块';用python,python-3.x,Python 3.x,我发现缩进错误 缩进错误:在审核日第33行应出现缩进块= 周2[“日历.星期一”] 这是代码,错误在最后一行,我使用的是最新的python3.7版本 # The calendar can give info based on local such a names of days and months (full and abbreviated forms) for name in calendar.month_name: print(name) for day in calendar.d
# The calendar can give info based on local such a names of days and months (full and abbreviated forms)
for name in calendar.month_name:
print(name)
for day in calendar.day_name:
print(day)
# calculate days based on a rule: For instance an audit day on the second Monday of every month
# Figure out what days that would be for each month, we can use the script as shown here
for month in range(1, 13):
# It retrieves a list of weeks that represent the month
mycal = calendar.monthcalendar(2025, month)
# The first MONDAY has to be within the first two weeks
week1 = mycal[1]
week2 = mycal[2]
if week1[calendar.MONDAY] != 0:
auditday = week1['calendar.MONDAY']
else:
# if the first MONDAY isn't in the first week, it must be in the second week
auditday = week2['calendar.MONDAY']
print("%10s %2d" % (calendar.month_name[month], auditday))
您在上一节中错过了一个缩进块
else:
# if the first MONDAY isn't in the first week, it must be in the second week
auditday = week2['calendar.MONDAY']
所以整个代码看起来像
for name in calendar.month_name:
print(name)
for day in calendar.day_name:
print(day)
# calculate days based on a rule: For instance an audit day on the second Monday of every month
# Figure out what days that would be for each month, we can use the script as shown here
for month in range(1, 13):
# It retrieves a list of weeks that represent the month
mycal = calendar.monthcalendar(2025, month)
# The first MONDAY has to be within the first two weeks
week1 = mycal[1]
week2 = mycal[2]
if week1[calendar.MONDAY] != 0:
auditday = week1['calendar.MONDAY']
else:
# if the first MONDAY isn't in the first week, it must be in the second week
auditday = week2['calendar.MONDAY']
print("%10s %2d" % (calendar.month_name[month], auditday))
从下一次开始,尝试使用Python的IDE,如PyCharm,它将突出显示缩进错误的语法,如if循环,else循环也应缩进四个空格
if week1[calendar.MONDAY] != 0:
auditday = week1['calendar.MONDAY']
else:
# if the first MONDAY isn't in the first week, it must be in the second week
auditday = week2['calendar.MONDAY']
谢谢python非常有帮助地指出-缩进行-它应该与第30行对齐(如果语句后的那一行),您需要更正
之后最后一条语句的缩进,否则
,请检查下面我的答案!您可能需要改写您的答案-循环可能不是描述条件语句的正确词语