Ad

Skip An Iteration Of The For Loop In Python

- 1 answer

I need a hand, I don't understand how to fix it.

Look at the photo to better understand my speech, where the new field is populated the value of new is subtracted from the total and I don't want it to be subtracted immediately but the next round.

How can I do?

def UserAndamentoListView(request):
    giorni = []
    mese = []
    new_user = []
    tot_user = []
    tot =  User.objects.all().count()

    for day in range(5):
        giorni.append(datetime.datetime.now() - datetime.timedelta(days=day))
        new = User.objects.filter(date_joined__contains = giorni[day].date())
        new_user.append(new)
        tot -= new.count()
        tot_user.append(tot)
    
    context = {'giorni': giorni, 'new_user':new_user, 'tot_user': tot_user}
    return render(request, 'andamento.html', context)

enter image description here

Ad

Answer

Just subtract after appending the total

for day in range(5):
    giorni.append(datetime.datetime.now() - datetime.timedelta(days=day))
    new = User.objects.filter(date_joined__contains = giorni[day].date())
    new_user.append(new)
    tot_user.append(tot)

    tot -= new.count()

The value of tot is changed at the end of the current loop iteration but is not used until the next trip around the loop.

Ad
source: stackoverflow.com
Ad