Python - Increment lower range boundary in a for loop

Viewed 270

I'm trying to gradually increase the lower boundary of a range during a for loop. This is supposedly how it would work (I'm new to Python so assume complete ignorance):

start = 0
reset = bool

for i in range(start, 6):
    print(i)

    if i > 4:
        reset = True

    if reset == True:
        start += 1
        i = start

Ideally, this would have outputs like:

0, 1, 2, 3, 4, 5 reset

1, 2, 3, 4, 5 reset

2, 3, 4, 5 *reset

I tried the above code but to no avail..

etc.

This must be possible to do, no? Note I'm trying to use this in a much more complicated nested 'for' loop and so ideally the solution would work with that.

Thanks in advance!

5 Answers

Try:

for i in range(0, 6):
    for j in range(i,6):
        print("{}, ".format(j))

Output:

  • i = 0 -> j = 0 1 2 3 4 5

  • i = 1 -> j = 1 2 3 4 5

  • i = 2 -> j = 2 3 4 5

  • i = 3 -> j = 3 4 5

and so on...

will not work! the first time for enters, it evaluates "range(start, 6)" and uses that result! so even if you change "start" after it is already done!

what you can do it is to use a while loop for your purpose

start = 0  
reset = bool

while start<6:
    print(i)

    if i > 4:
        reset = True

    if reset == True:
        start += 1
        i = start

Changing the loop control variable will do you no good here because:

for i in some_range:
    do_something_with(i)

is effectively the same as (Python-like pseudo-code):

i = some_range.start
while i != some_range.end:
    do_something_with(i)
    i = some_range.next

In other words, regardless of the changes you make to i within the loop, the loop itself will throw that away on the next iteration.

Something like this is probably a good starting point:

# Starting limits.

start = 0
stop = 6

# Outer loop responsible for resets.

while start < stop:
    # Disallow reset to start with.

    reset = False

    # Inner loop responsible for a single sequence.

    for i in range(start, stop):
        print(i, end=' ')

         # Whatever rule you need to trigger reset.

        if i > 4:
            reset = True

        # The actual reset that sets up and starts the next sequence.

        if reset == True:
            print()
            start += 1
            break

In that code, you actually have an outer loop that manages the resets. When it comes time to reset, you simply modify the parameters for the next outer loop and just break out of the inner loop:

0 1 2 3 4 5    
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

Issue

A single for loop won't work since range(start, 6) creates an iterator based upon the initial start value which i is sequencing through.

Changing start afterwards has no effect on this sequence of numbers.

Try single while loop

start = 0
i = start
while i < 6:
  print(i)
  i += 1

  reset = i > 4

  if reset:
    print('reset')
    start += 1
    i = start

Output

0
1
2
3
4
reset
1
2
3
4
reset
2
3
4
reset
3
4
reset
4
reset
5
reset

You can't update a range once it is declared. Alternatively use while and change the lower range at the end of the loop. You can have a separate variable start for initializing the i every time.

print("") is just a line break

k = 6
i = 0
start = 0
while i < k:
    print(i, end="")
    i += 1
    if i == k:
        print("")
        start += 1
        i = start
Related