i am trying to create a program that uses * to make triangles depending on how big the user wishes to make them. This is the code i have so far however have no idea where to go from here.. can anyone help?? thankyou

def triangle():
        totalRows = int(eval(input("Please enter a number: ")))
    for currentRows in range(1,totalRows+1),:
        for currentCol i range (1, currentRow+1):
                        print("*", end = " ")

main()

Recommended Answers

All 4 Replies

def triangle():
  totalRows = int(input("Please enter a number: "))
  for totalRow in range(1, totalRows + 1):
    print '*' * (totalRow)
      
triangle()

Something like that?

its getting there thankyou I need it to look like this
*
**
***
****
*****
****
***
**
*
And the number entered determines how many rows of asterics there are example the triangle above would be the result of 5 being entered by the user. Any other tips?? thankyou

Your first code was a big mess.

def triangle():
    totalRows = int(input("Please enter a number: "))
    for totalRow in range(1, totalRows):
        print '*' * (totalRow)
    for totalRow in range(totalRows, 0, -1):
        print '*' * (totalRow)

triangle()

If you want to do it fancy way look use yo-yo for (I posted it once to code snippets but can not find it there, so here little censored version of my version of the diamond.

def diamond(width):
    width = width - 1 if not (width & 1) else width # round down to odd number
    print "Width %i" % width
    for stars in range(1,width+1,2) + range(width-2,0,-2):
# number of space formula removed, replace 0 with the formula based on number of stars
        print 0 *  ' ' + stars * '*' 

diamond(12)

""" Output:
Width 11
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.