1

I have this task to do :Write a procedure called triangle that takes in a number and then prints out a triangle of that height.Like this:

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

The only solution I found was this code :

def triangle(size):
    spaces=""
    stars=""
    line=""
    for i in range(0,size):
        for j in range(0,(size-1-i)+11-(size-1-i)-i): 
            line=line+" "

        for k in range(0,2*i+1):
            line=line+"*"

        print(line)
        line=""

    triangle(2)
    triangle(3)
    triangle(4)

I just want to know does anyone have a simpler way of doing this or a way to simplify this code and making it more readable?

3
  • What's wrong with this? This code pretty much simple and readable to me...and self explanatory as well. Commented Dec 31, 2015 at 7:29
  • i understand the Structure , But I don't understand how the person worked out the calculations to form the triangle e.g line 6 Commented Dec 31, 2015 at 9:36
  • Don't try to understand the calculus of line 6, as it is bugged. Try calling triangle(15) for example. Commented Dec 31, 2015 at 9:44

3 Answers 3

9

Here is a one-liner that will probably prove your teacher you copied from internet:

Edited following OP edit:

def triangle(n) : print('\n'.join(map(lambda i:('{:^'+str(2*n)+'}').format('*'*(2*i+1)), range(n))))
Sign up to request clarification or add additional context in comments.

9 Comments

And here is my funnier one-liner : def triangle(n) : print('\n'.join(map(lambda i:'*'*(2*i+1), range(n))))
And here is a centered triangle: def triangle(n) : print('\n'.join(map(lambda i:('{:^'+str(2*n)+'}').format('*'*(2*i+1)), range(n))))
Can you replace the answer as it currently stands with your last comment here? As it is, it doesn't actually answer the question.
OP reformatted the question. I've edited my answer to give the correct answer. :)
Idiot me sorry. ;) and Thanks p.s. Can you explain line 6 for me please I don't know how the person done the calculations.
|
2

As you're using Python 3, you have some nice options with the print() function:

def triangle(size):
    print(*(('{:^'+str(2*size+1)+'}').format('*'*row) for row in range(1, 2*size+1, 2)), sep='\n')

This creates a formatted string that centers the proper number of stars for each row, and then sends each row as an argument to print(), with a newline as a separator.

>>> triangle(4)
    *
   ***
  *****
 *******
>>> triangle(5)
     *
    ***
   *****
  *******
 *********

Comments

2
def triangle(line):
    for numberofsymbol in range(line):
        for j in range(line-numberofsymbol): print(" ", end='')
        for j in range(numberofsymbol): print("* ", end='')
        print("")

triangle(10)


OUPUT:
         * 
        * * 
       * * * 
      * * * * 
     * * * * * 
    * * * * * * 
   * * * * * * * 
  * * * * * * * * 
 * * * * * * * * * 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.