How to run the loop again with range change based on 'if statement'?

let us say.

a = 1
b = 10
for i in range(a, b):
 print(i)
 if i == 5:
 a +=1 # and run loop again.

How would I increase a by 1 and run the loop again?
Is there any way?

Hello@acc1444.

Perhaps this is something you want:

def decreasing_range(a,b):
    while a < 5:
        a += 1
        print(”\n”)
        # just to make space between ranges. 
        for i in range(a, b):
            print(i)

decreasing_range(1, 10)

?

1 Like

EDIT: @ulfen69 solution is better.

One way, that I can think of is to create a function with a loop inside. Although full disclosure this might not be the best solution. It depends on the “why”.

a = 1

def looper_func(min, max, stop):
    for i in range(min, max):
         print(i)
         if i == stop:
            # I could return i, but we already know where we want to stop
            return True   

""" 
Instead of:

if looper_func(a, 10, 5) == 5:

you just use the boolean, like below.
"""

if looper_func(a, 10, 5):
   a += 1
   looper_func(a, 10, 5)
   

But, like I wrote, I’m not a 100% on this implementation. If this is part of a potential solution for a more abstract problem, it would help to know more details about that.

1 Like

Hi @zberwaldt.

Thanks for the ”like”. :slightly_smiling_face:
Your solution was good too.
I got a idea how to improve my solution.
I just added a ’c’ to my parameters, change the number(5) to ’c’ and then I also have a ”stop” value I can put as an argument.

1 Like

Hi @acc1444 you can do it with recursion, like this:

def ranger(min, max, stop):
    for i in range(min, max):
        print(i)
        
        if i == stop:
            min += 1
            return ranger(min, max, stop)

ranger(1, 10, 5)

The output:

1
2
3
4
5
2
3
4
5
3
4
5
4
5
5
6
7
8
9

Recursion means you call a function by itself. It’s just a play here, I don’t know if it would be good practice for production code, because there is a thing call recursion depth, that means your code will blow up when it reaches the limit how many times it can call themselves. Here is some discussions about it: https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it
and: https://stackoverflow.com/questions/33155862/is-it-bad-practice-to-use-recursion-where-it-isnt-necessary

2 Likes

I fixed your post so that you have [code] [/code] around your code.

Everyone here has interesting solutions, but the simplest is just use two for-loops:

b = 10
for a in range(0, b):
    for i in range(a, b):
        print(i)

If you want to get really crazy you can do one for b too:

for b in range(0, 10):
    for a in range(0, b):
        for i in range(a, b):
            print(i)

Remember that in programming you can usually “nest” things inside other things as much as you want.

1 Like

Hi @zedshaw I first thought about this too, but how do you make it stop at 5 and start the loop over with start incremented with 1? I just didn’t figured it out, so I came to the solution with the recursion. Do you have an idea how to do this with just loops?

To clarify: I understand @acc1444 question in that way that he wanted an output like this, but maybe I’m wrong.

1 # 1 = a
2
3
4
5 # stop here
2 # start over with a + 1 = 2
3
4
5 # stop here
3 # start over with a + 1 = 3
4
5
4
5
5
6
7
8
9

Yeah, it’s not too clear what needs to be done, but if you need to totally control the loop then you’d use a while-loop instead of a for-loop. All the solutions are close, so it depends on what’s actually needed for the problem.

Okay, thanks. Good hint. Now suddenly I have a different solutions in my mind :slight_smile: it’s good to have a pro around :+1: