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()``````
4
Contributors
4
Replies
9
Views
7 Years
Discussion Span
Last Post by pyTony
``````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()``````

Edited by snippsat: n/a

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
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
"""``````

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.