2

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!

3
  • Not possible since range(start, 6) creates an iterator based upon the initial start value which i is sequencing through. Changing start afterwards has no affect on this sequence of numbers. You need a while loop. Commented May 15, 2020 at 13:16
  • You can use a combination of while and for loop for this. Commented May 15, 2020 at 13:16
  • A for loop in Python doesn't care about the current value of x (unlike most C-like languages) when it starts the next iteration; it just remembers the current position in the range and assigns the next value. In order to be able to manipulate the loop variable, you need to use a while loop, which doesn't exert any control over any variables (it just evaluates the condition you give it). Commented May 15, 2020 at 13:19

5 Answers 5

2

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...

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.