Miryloth
Miryloth

Reputation: 55

How to print a triangle using for loops

I need some help trying to solve this, right now it keeps printing vertically only

I need it to come out as

height = int(input("Height of triangle: "))
for x in range(height):
   for y in range(height):
     print("#",end = '')
     print()

Upvotes: 3

Views: 1211

Answers (2)

user1308345
user1308345

Reputation: 1112

height = 6

for rowIndex in xrange(height-1):
    row = [' ']*height            # yields array of size height
    row[0] = row[rowIndex+1] = '#'
    print (''.join(row))    # This makes string from array
print ('#'*height)          # Print lower side of triangle

You can also remove "+1" on line 5 to get more "edgy" triangles.

Upvotes: 0

Tim
Tim

Reputation: 106

Here's my solution, it involves the use an of accumulator:

height = int(input("Height of triangle: "))
count = 0

for i in range(height-1):
    print('#' + ' '*count + '#')
    count += 1

print('#'*height)

Upvotes: 2

Related Questions