This is an example of a correct pyramid.
I have this exercise as homework but I cant figure it out.
The code i've tried so far is this one and I can't make it inverted:
n = 5
for i in range(n, 0, -1):
for j in range(n, i-1, -1):
print(j, end=" ")
print(" ")
CodePudding user response:
n = int(input("What's the number: "))
for i in range(n, 0, -1):
for j in range(i, 0, -1):
print(j, end="")
print()
Works for me.
The trick is to start the second loop "j" with the decrementing "i" downwards.
Imagine you are doing not a pyramid, but printing
5 4 3 2 1
n times.
Something like
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
...
You would start i and j at the same index, n, and just repeat the task.
Here, you start j at i, so each time i gets decremented, j starts from the decremented i, thus making a pyramid effect.
Hope you understand :)