I was working through another question here and have hacked up an answer:
def get_rows(): """ Get height of triangle from user""" while True: rows = input('Enter the number of rows: ') if 3 <= rows <= 33: return rows def triangle(rows, invert=True): """ Print the outline of a triangle, of variable size. Print out a regular or inverted version of the triangle as necessary """ if invert: height = -1 * rows else: height = 0 # inner padding for min height (3) inner_buffer = [0, 1, 3] while len(inner_buffer) <= rows: inner_buffer.append(inner_buffer[-1]+2) level = 0 while level <= rows: outer_padding = ' '*(rows - abs(height)) if height == 0: print(outer_padding + '*') else: inner_padding = ' '*( inner_buffer[ abs(height) ] ) print(outer_padding + '*' + inner_padding + '*') height += 1 level += 1
At this stage I am working to re-implement this two ways using: recursion and
But I am looking at this and wonder if it would be a good place to use decorators. If someone is happy to implement one triangle (either regular or inverted, I don't mind) I would love to see the difference in the 3 implementations.