I am currently working on a homework assignment to generate what is known as Pascal's triangle in Python.
So far, this is what I have:
def mytri(myrange):
trianglevar = [[1]]
for i in range(1, myrange):
tempvar = [1]
for n in range(0, i-1):
tempvar.append(trianglevar[i-1][n]+trianglevar[i-1][n+1])
tempvar.append(1)
trianglevar.append(tempvar)
return trianglevar
def mymenu():
for i in mytri(int(raw_input("Please enter the height of the triangle: "))):
print i
print '\n'
choicevar = raw_input("Would you like to create another triangle? (y/n): ")
if choicevar == "y":
mymenu()
else:
print "Goodbye."
mymenu()
What the program does up to this point is perform the calculation for the triangle. It calculates the numbers in each row (starting with 1), and stops after reaching the number of rows specified by the user.
However, I'm not sure how to format my triangle. It currently prints as:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
...etc.
The output I want is:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
...etc.
(It's a bit off due to the brackets/commas, but I'm just trying to get the general format down right now.)
Thank you for any help you can offer!