gumelaragum gumelaragum - 3 months ago 12
Pascal Question

How to make triangle x in python from source code below

Hello I want to ask everybody...
Like this...
I want to make a triangle X or *, like below:

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


My Algorithm is like this:

for y:=1 to i do
for x:=1 to j do
for j-x to 1 do write(' ');
for i to 2*(x-1)+1 do write('*');


Can anybody tell me how is the source code for python like pascal in above?
Thanks for your answer

Answer

You can simply do this like that:

def triangle(lines):
    for i in range(lines):
        print(' '*(lines-i) + '*'*(i*2+1))

triangle(5)

Output as expected

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