Can we change Python for loop range (higher limit) at runtime?

PythonProgramming

No, You can't modify a range once it is created. Instead what you can do is use a while loop instead. For example, if you have some code like:

for i in range(lower_limit, higher_limit, step_size):

# some code
if i == 10:
   higher_limit = higher_limit + 5

You can change it to:

i = lower_limit
while i < higher_limit:
   # some code
   if i == 10:
      higher_limit = higher_limit + 5
   i += step_size
raja
Published on 20-Apr-2018 10:23:49
Advertisements