Printing multiple triangles I am new to Python and only just started learning about functions. I stumbled upon a question which recommends using function to print triangles in such a format:

Enter height: 5

         /\
        /  \
       /    \
      /      \
     /________\
    /\        /\
   /  \      /  \
  /    \    /    \
 /      \  /      \
/________\/________\

Enter height: 2

   /\
  /__\
 /\  /\
/__\/__\


Although I have gone through and successfully coded for the requirements. I feel though there needs to be a better approach. I was wondering if someone could review my code and point me in the right direction as to how I might improve on it.

This is what I currently have:

import sys

def triangle(height):
    print()
    max = (height * 2) - 1
    mid = 0
    while max &gt; height:
        statement = " " * max + "/" + " " * mid + "\\"
        print(statement)
        max -= 1
        mid += 2
    statement = " " * max + "/" + "_" * mid + "\\"
    max -= 1
    print(statement)
    small = 0
    while max &gt; 0:
        statement = " " * max + "/" + " " * small + "\\" + " " * mid + "/" + " " * small + "\\"
        print(statement)
        mid -= 2
        max -= 1
        small += 2
    statement = " " * max + "/" + "_" * small + "\\" + " " * mid + "/" + "_" * small + "\\"
    print(statement)
    pass

if __name__ == "__main__":
    userInput = input("Enter height: ")
    try:
        userInput = int(userInput)
    except ValueError:
        sys.exit("\nInvalid height.")
    if userInput &lt; 2 or userInput &gt; 20:
        sys.exit("\nInvalid height.")
    triangle(userInput)


I am curious as to there is a way to only code for one triangle and print multiple.
Your code seems to be working fine, but I have some suggestions:


Make sure that no line is longer than 80 characters
Instead of exiting when input is &lt;2 or &gt;20, instead prompt for a new input (your code also works for input 1, but not for 0)
Instead of the function handling the printing make it return the statement string
Avoid having variables with the names of builtins, e.g. max
The last pass doesn't add any functionality


To further improve your solution, I would do one of two things:

Continue with the same logic, but reduce the cluttering

I would definitely change from using while loops to using for loops. With a bit of math, the function can be reduced to:

def create_triangle(n):
    triangle = ""
    for i in range(n):
        triangle += ' '*(2*n-i-1) + '/' + ' _'[i==n-1]*2*i + '\\' + "\n"
    for i in range(n, 2*n):
        triangle += ' '*(2*n-i-1) + '/' + ' _'[i==2*n-1]*(2*i-2*n) + '\\'
        triangle += ' '*(4*n-2*i-2) + '/' + ' _'[i==2*n-1]*(2*i-2*n) + '\\'
        triangle += '\n'
    return triangle


Most of it is similar to what you have in your code, the only new thing being ' _'[i==n-1]*2*i. This statement could be hard to interpret, but the only thing it does is to print _ when i==n-1, and a space otherwise. 

Change to a more general approach for these kinds of problems

If you want to solve more complex problems related to creating patterns with strings, I'd instead use an array based solution. A function like this:

def create_print_array(n):
    arr = [['*' for i in range(n)] for j in range(n)]
    return '\n'.join([''.join(row) for row in arr])


returns a pattern like this:

*****
*****
*****
*****
*****


for input n = 5. Since we have a 2d matrix, we can directly manipulate the values to form our desired output. With a bit of math again, the function can be written as:

def create_triangle_array(n):
    # create a 2D array filled with spaces
    arr = [[' ' for i in range(4*n)] for j in range(2*n)]
    # create the outer triangle legs
    for i in range(2*n):
        arr[i][2*n-i-1] = '/'
        arr[i][2*n+i] = '\\'
    # create the inner triangle legs
    for i in range(n, 2*n):
        arr[i][i] = '\\'
        arr[i][4*n-i-1] = '/'
    # create the bases for all three triangles
    for i in range(2*n-2):
        arr[n-1][n+1+i] = '_'
        arr[2*n-1][2*n+1+i] = '_'
        arr[2*n-1][1+i] = '_'
    # join the array into a string
    return '\n'.join([''.join(row) for row in arr])


Both of these approaches give the same form on the output, but it should be noted that the second approach gives some extra spaces on the end of each line, to make each line equally long. Both approaches also work for input &gt;= 0 (I left out error-handling for input &lt; 0 since you already covered that in your code).

As a final note, I'd say that your code looks very good for a beginner. It is easy to follow, and you have covered potential wrong input from the user.