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

How to inverse the result of this pyramid in python?

We have an exercise for our programming class and we have to inverse the result of this code.

basis:

line = 8
while line >= 1:
    number = 1
    while number <= line:
        print(number, end = '')
        number = number + 1
    line = line - 1
    print('')

output:

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

12345678
1234567
123456
12345
1234
123
12
1

I’ve been struggling to get it to look like this:

87654321
7654321
654321
54321
4321
321
21
1

Any help would be appreciated!

>Solution :

Just invert your loop (nested while) invariant:

line = 8
while line >= 1:
    number = line
    while number > 0:
        print(number, end='')
        number = number - 1
    line = line - 1
    print('')

Output:

87654321
7654321
654321
54321
4321
321
21
1
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