Python How to make a number triangle

Question:

So I’ve recently started learning python and my friend gave me a changllege. How to make a number triangle. So basically, I need to make python let me input a number, for example 5. After I do that, I want python to print

54321
4321
321
21
1

But because I am new to python, I don’t know how to do it.
So far, I’ve got this:

x = int(input('Enter a Number: '))

for i in range(x,0,-1):
  print(i,end='')
  if i != 0:
    continue
  else:
    break

And the output is:

Enter a Number: 5
54321

Any ideas how to make it print
54321
4321
321
21
1
?
Thanks for any advice

Asked By: Kurtis

||

Answers:

rows = int(input("Inverted Right Triangle Numbers  = "))

print("Inverted")

for i in range(rows, 0, -1):
    for j in range(i, 0, -1):
        print(j, end = ' ')
    print()

Output

5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1
Answered By: Bhargav
def trig_print(number):
    if number > 0:
        for i in range(number):
            print(number - i,end='')
        print('')
        number = number - 1
        trig_print(number)
    else:
        return 0

trig_print(5)
Answered By: 0to1toeverything

Here is a sample code for you.

Short version (suggest learning about list comprehension and join functions):

x = int(input('Enter a Number: '))
for i in range(x, 0, -1):
    print(''.join([str(j) for j in range(i, 0, -1)]))

Longer version which is easier to understand:

for i in range(x, 0, -1):
    s = ''
    for j in range(i, 0, -1):
        s += str(j)
    print(s)

Output:

54321
4321
321
21
1
Answered By: Quan Nguyen

I used to solve it by while loop.

3 step

  1. input initial number.
  2. make list one to initial number.
  3. reverse list and print it.
>>> x = int(input('Enter a Number: '))
Enter a Number: 5
>>> while x >0 : 
    l=list(range(1,x+1)) # range function
    l.reverse()  # list.reverse function
    print(' '.join(map(str,l))) # join and map function
    # I recomanded input space between number for readability.
    x -= 1 
    
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Answered By: student
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.