Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading