dimanche 10 mai 2020

How to run a for loop with variable range?

I want to have a Python program like--

for i in range(r):
    if (i==2):
        #change r in some way

which will run the loop for the new range r, after it gets modified in the if statement.

Even if I change r after the if statement,the for loop runs for the initial r I gave.This must be happening because range(r) gets fixed in the for statement in the first line itself,and is not affected by change in r later on.

Is there a "simple way" to bypass this?

By "simple" I mean that I don't want to add a counter which counts how many times the loop already ran and how many times it need to run again after changing(specifically increasing) r,or by replacing for loop with a while loop.

Aucun commentaire:

Enregistrer un commentaire