Danny74
Danny74

Reputation: 11

Python numbers triangle

I am trying to produce a balanced triangle but I got halfway there. I want the result in this format

1
12
123
1234
12345
1234
123
12
1

Here Is the code I have so far:

def numpat(n):       
    num = 1
    for i in range(0, n): 
        num = 1
        for j in range(0, i+1): 
            print(num, end=" ") 
            num = num + 1
        print("\r") 
n = 7
numpat(n)

Upvotes: 0

Views: 72

Answers (1)

Dario
Dario

Reputation: 71

First thing, try to use code tags in the questions because otherwise the spacing will not be clear. Anyway you should conver num to a string in order to add digits to the end. Try something like:

def numpat(n):
    num = ""
    for j in range(1, n+1):
        num += str(j)
        print(num, end=" ")
        print("\r")
    for j in range(len(num)):
        num = num[0:-1]
        print(num, end=" ")
        print("\r")`

Upvotes: 1

Related Questions