Как исправить ‘IndentationError: ожидаемый блок с отступом’ в python

#python-3.x

#python-3.x

Вопрос:

я нашел IndentationError

IndentationError: ожидаемый блок с отступом в строке 33 в auditday = week2[‘календарь.ПОНЕДЕЛЬНИК ‘]

вот код, ошибка на предпоследней строке, я использую последнюю версию 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("s -" % (calendar.month_name[month], auditday))
  

Комментарии:

1. Как так услужливо указывает python — сделайте отступ в строке — он должен совпадать со строкой 30 (после if утверждения)

2. Вам нужно исправить отступ последнего оператора после else , проверьте мой ответ ниже!

Ответ №1:

Как и в цикле 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']
  

Спасибо

Комментарии:

1. Возможно, вы захотите изменить свой ответ — цикл, вероятно, не подходит для описания условных операторов

Ответ №2:

Вы пропустили блок отступа в последнем else.

 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("s -" % (calendar.month_name[month], auditday))
  

В следующий раз попробуйте использовать IDE для Python, например PyCharm, который выделит синтаксис, в котором вы получаете ошибки отступа