python-3.x - 如何在 python 中修复“IndentationError:预期有缩进块”
问题描述
我发现缩进错误
IndentationError: 在 auditday = week2['calendar.MONDAY'] 的第 33 行需要一个缩进块
这是代码,错误在倒数第二行,我使用的是最新的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.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))
解决方案
和 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']
谢谢
推荐阅读
- reactjs - MUI 中的闪烁动画
- multithreading - JMeter 5.0:在树侦听器中查看结果仅显示最后 100 个线程的结果
- ios - react-native pjsip 模块 - iOS 背景没有接收
- spotify-docker-client - 如何将卷挂载到容器?
- sas - SAS:使用索引列作为提示的哈希表查找
- css - 仅使用 CSS 从中心旋转三角形图像?
- azure - 在 Azure AD Enterprise 应用程序中跳过 Home Realm Discovery Page 或 Microsoft 登录页面
- javascript - 函数调用内的javascript内联注释
- javascript - 使用最新 jQuery 的视差滚动不起作用
- r - 为 mlogit 设置 data.frame - 错误消息:`row.names<-.data.frame`(`*tmp*`, value = paste(chid, alt, sep = ".")) 中的错误: