Home > other >  How to print list of numbers but counting in like a pyramid fashion in IDLE
How to print list of numbers but counting in like a pyramid fashion in IDLE

Time:11-07

I'm trying to make a loop that prints numbers exactly like this:

1
12
123
1234
12345

I already have this pattern down for different characters, and can even print this:

1
22
333
4444
55555

But I'm having a big headache trying to figure out how I can make it count. Any help would be greatly appreciated.

Here is the code I have to print the list above:

for row in range (number_of_rows   1):
    for column in range(row)
         print (row, end='')
print()

CodePudding user response:

Sorry, I was just going to make a comment, but this will be easier:

for row in range (number_of_rows   1):
  for column in range(row)
       print (column 1, end='') #<-- put column here instead of row and add a " 1"
  print()

Some more details of what is going on:

for row in range (number_of_rows   1):

Iterate from zero to number_of_rows. e.g. if number of rows was 5 this would iterate through a row of 0,1,2,3,4,5

for column in range(row):

For each row iterate from 0 to the row number minus 1. e.g. row 3 would iterate through 0, 1, 2

print (row 1, end='')

Print the column, one digit at a time. To start the row at 1 we need to add 1

CodePudding user response:

If I'm understanding correctly, you could do something like

for i in range(1, rows   1):
  print(''.join(str(j) for j in range(1, i   1)))

outputs:

1
12
123
1234
12345

CodePudding user response:

did you mean like this triangle

def triangle(pos1):
    n=pos1
    k = n - 1
    for i in range(0, n):
        for j in range(0, k):
            print(end=" ")
        k = k - 1
        for j in range(0, i 1):
            print(str(j) ' ', end="")
        print("\r")
rows=8
triangle(rows)

output

       0
      0 1
     0 1 2
    0 1 2 3
   0 1 2 3 4
  0 1 2 3 4 5
 0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
  • Related