По дате я пытаюсь узнать первый и последний календарные дни месяца.
Код: Выделить всё
import datetime
import calendar
def calendar_days_month(run_date):
"""
:param run_date: date on which the process is running
:return:
"""
d = datetime.datetime.strptime(run_date, '%Y-%m-%d').date()
first_calendar_day = d.replace(day=1).strftime("%Y-%m-%d")
res = calendar.monthrange(d.year, d.month)[1]
if len(str(d.month)) == 1:
last_month = '%02d' % d.month
last_calendar_day = str(d.year) + '-' + str(last_month) + '-' + str(res)
return first_calendar_day, last_calendar_day
abc_date = '2024-10-31'
test_1, test_2 = calendar_days_month(abc_date)
print(test_2)
print(test_1)
Код: Выделить всё
Traceback (most recent call last):
File "/main.py", line 21, in
test_1, test_2 = calendar_days_month(abc_date)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/main.py", line 16, in calendar_days_month
last_calendar_day = str(d.year) + '-' + str(last_month) + '-' + str(res)
^^^^^^^^^^
UnboundLocalError: cannot access local variable 'last_month' where it is not associated with a value
Подробнее здесь: https://stackoverflow.com/questions/791 ... -in-python