Liam Emery - 1 year ago 67
Python Question

# How do I make a triangle of numbers using python loops?

I am trying to achieve this

``````0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
``````

And I'm getting close but now I'm stuck. Here is my current code

``````def triangle():
n = 9
numList = [0,1,2,3,4,5,6,7,8,9]
for i in range(10):
for i in numList:
print(i, end="  ")
print()
numList[n] = 0
n -= 1
triangle()
``````

And this is the current output

``````0  1  2  3  4  5  6  7  8  9
0  1  2  3  4  5  6  7  8  0
0  1  2  3  4  5  6  7  0  0
0  1  2  3  4  5  6  0  0  0
0  1  2  3  4  5  0  0  0  0
0  1  2  3  4  0  0  0  0  0
0  1  2  3  0  0  0  0  0  0
0  1  2  0  0  0  0  0  0  0
0  1  0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0  0  0
``````

So I'm there in a round about way, except, its backwards, and there is 0's instead of spaces

interesting puzzle, you could try this:

``````n = range(0,10)    #set your range
while len(n)<20:   #loop until it exhausts where you want it too
print ''.join(str(e) for e in n[0:10])  #print as a string!
n = [' ']+n      #prepend blank spaces
``````

here is an example

You could apply the same logic to your attempt. Basically I add a space to the beggining of N after each loop and then print only the first ten elements. The way I print the list is a little clunky because I am joining, I need to change each element to a string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download