How to change for loop variables in python/django while for loop is running

  date, datetime, django, for-loop, python

I want to change variables that define a for loop while the loop is running. It will make more sense when you see the code, so here it is:

days = form.instance.days

for i in range(0, days + 1):
    days_added = i
    current_end = class_start_date + datetime.timedelta(days=days_added)
    current_end_day = calendar.day_name[datetime.datetime.strptime(str(current_end), '%Y-%m-%d').weekday()]
    if current_end_day == 'Saturday' or current_end_day == 'Sunday':
        days = days + 1

You see, when I run the code days = days + 1, I want the days for for i in range(0, days + 1): to be updated, so that the number of total loops of the forloop will be increased by 1 whenever days = days + 1 occurs. days = form.instance.days is increased by 1, but days in for i in range(0, days + 1): is not updated. I hope you guys could help. Thanks.

Source: Python Questions

LEAVE A COMMENT