-1

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!

tripleee
  • 175,061
  • 34
  • 275
  • 318
Stick1000
  • 25
  • 5
  • 1
    Hint: `str.center` – tobias_k Sep 18 '18 at 10:10
  • You get error at line 9 because you are using Python3 ,and the line needs to be: `print(space,asterisk)` – HaR Sep 18 '18 at 10:13
  • @HaR I'm using Python 2.7.13. The error is "TypeError: unsupported operand type(s) for -: 'str' and 'int'". I – Stick1000 Sep 18 '18 at 10:17
  • 2
    @Stick1000 better not saying you got an error. Instead show the error, please (as an edit in your question). And also you mixed 4 spaces with 8 spaces indendation, see [editing-help](https://stackoverflow.com/editing-help). -- `space` or `side` is a string which needs to be converted to int. – colidyre Sep 18 '18 at 10:17
  • 1
    If you are learning Python in 2018, you should definitely be targeting the currently recommended and supported version of the language, which is Python 3. Version 2 was originally slated to be end-of-lifed already, although it got a couple of miserable extra years in terminal care. – tripleee Sep 18 '18 at 10:23
  • I rolled back your latest edit (it's still available from the [revision history](https://stackoverflow.com/posts/52384142/revisions)). Your question should remain strictly a question, though you are more than welcome to post an answer of your own. – tripleee Sep 18 '18 at 10:25

3 Answers3

0

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)
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
0

I write a new one in python3.5,And it may be suit for you.But it is not prefect

side = 4

def triangle(x):
    for y in range(1,x):
        s = 2*y - 1
        b = x - y
        print(" "*b+"*"*s)
triangle(side)

Output:

   *
  ***
 *****
KC.
  • 2,981
  • 2
  • 12
  • 22
0

Here is the code to have an isosceles triangle:

def print_triangle(n):
    for i in range(1, n+1):
        print (" "*(n-i), end=" ")
        print (("*"*i)+(i*"*"))

print(print_triangle(6)) 

Output:

      **
     ****
    ******
   ********
  **********
 ************
None
tdy
  • 36,675
  • 19
  • 86
  • 83
  • see [why is this printing "None" in the output?](https://stackoverflow.com/q/28812851/13138364) for why your code includes "None" in the output – tdy Nov 20 '21 at 17:23