Q:

Making triangles in Python

Below is a code I made in Python for our lab activity which makes a triangle.
side = input("Input side: ")

def triangle(x):
    print ((x - 1)*" "),"*"
    asterisk = "*"
    space = side
    for i in range(x):
            asterisk = "**" + asterisk
            space = (space-1) * " "
            print space,asterisk

triangle(side)

The output should be this:
  *
 ***
*****

I got the number of asterisks on each row correctly, but I cannot figure out how to make the necessary amount of spaces to make it look like a triangle. Using the above code, which I think might be the solution, always produces an error at line 9. Any help I appreciated. (I'm new here so if I violated some rules please let me know.) Thanks and good day!

A:

You are getting the error(TypeError) because you are assigning the space variable with a string type, and then you are subtracting it in the next loop. Edited code would look like this (In python 2.7) and Never mix 4 spaces and 8 spaces indentation.
side = input("Input side: ")

def triangle(x):
    print ((x)*" "),"*"
    asterisk = "*"
    spaces = side
    for i in range(x):
        asterisk = "**" + asterisk
        spaces -= 1
        print ' ' * spaces,
        print asterisk 

triangle(side)

