Home > Software design >  How to repeat value in middle of loop?
How to repeat value in middle of loop?

Time:03-23

When the loop reaches the max value I want to print it again and again for the number in max value, i.e. if max = 5 I want to reach the loop till 5 and the print 5 for 5 times and the continue in reverse order.

max=10
if max >= 0: 
    for i in range(1, max): 
        print(i) 
        if i == max: 
            for _ in range(5): 
                print(i) 
    else:
        print("x") 
        for i in range(max, 0, -1): 
            print(i) 

This is code I tried but it doesn't seem to repeat the max values

CodePudding user response:

is this what you are looking?

max=5
if max >= 0: 
    for i in range(1, max 1): 
        print(i) 
        if i == max: 
            for i in range(max): 
                print(max) 
    else:
        print("x") 
        for i in range(max, 0, -1): 
            print(i) 
  • Related