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
2
  • 4
    What do you think print('0',0,base) does? Have you tried print('0' * base)? Commented Feb 4, 2015 at 12:28
  • 1
    Though... the above comment by @tobias_k solved your problem for now... please focus on thinking why it solved the problem. Commented Feb 4, 2015 at 12:30

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.