Home > Blockchain >  Is there a way to repeat 'A number in the middle of a loop' multiple times?
Is there a way to repeat 'A number in the middle of a loop' multiple times?

Time:03-11

Let's say this is a loop I am aiming for, Starting with 10 and ending at 6.

for i in range(10,6,-1):
        print(i)

But I want to print 8 multiple times.

  • The output expected is 10, 9, 8, 8, 8, 7, 6

so if the loop is going on downhill, Is there a way to stop at a certain point and repeat just the value again and again for N number of times?

CodePudding user response:

Very simple one with hard-cording:

for i in range(10,5,-1):
        if i == 8:
            for _ in range(3):
                print(i)
        else:
            print(i)

Output:

10
9
8
8
8
7
6
  • Related