Search code examples
python-3.xloopsfor-loop

Execute the loop again if condition matches


Hi i am trying to run a loop again if condition matches
Below is my code if range matches to 10 then execute line-1 and line-2 again.
The complete below loop has to run for 50 times

    for x in range(1,11): #line - 1
        print(x)          #line - 2
        if x==10:
            <excute Line -1 & Line -2>

Any modifications , please suggest.
thanks in advance


Solution

  • If you want your for loop to repeat 50 times, the easiest thing is to put it inside a for loop.

    for _ in range(50):
        for j in range(10):
            print(j+1)
    

    Though, I would probably use one loop and the modulo operator % to give the same result.

    for j in range(50 * 10):
        print(j % 10 + 1)