Home > Enterprise >  How to inverse the result of this pyramid in python?
How to inverse the result of this pyramid in python?

Time:09-13

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:

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!

CodePudding user response:

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
  • Related