2

I need this nested loop to work and its simple

def triangle():
      print("Making a Triangle")
      base = 6
      while base > 0:
            print('0',0,base)
            base = base - 1
 triangle()

My current output is:

Making a Triangle
0 0 6
0 0 5
0 0 4
0 0 3
0 0 2
0 0 1

I need my output to look like this:

000000
00000
0000
000
00
0
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

1 Answers1

4

You can use the multiplication * operator to create a string by repeating a character. Also, this would be a very straight forward application for a for loop.

def triangle(n):
    print('making a triangle')
    for zeroes in range(n):
        print('0' * (n-zeroes))

Testing

>>> triangle(6)
making a triangle
000000
00000
0000
000
00
0

Although if you'd like to stick with a while loop, you could do

def triangle(n):
    print('Making a triangle')
    while n > 0:
        print('0' * n)
        n -= 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218