Reputation: 31
I have this code to print pascal triangle with N lines (N represents the user input of how many lines they want). I have the format needed, but I don't know how to make the code work.
I'm new to python and programming in general, and really struggling with functions
. I don't know how to write arguments for functions, and how to pass them properly.
the problems is from the second function onwards (def pascal_triangle_tostring()
). I can get the input and generate the list. But from here I'm not sure what to do.
#should take user input and validates it
def get_triangle_size():
while True:
numberOfRows = input("Enter the no. of rows: ")
try:
numberOfRows = int(numberOfRows)
except ValueError:
print("N must be an integer")
continue
if numberOfRows <= 0:
print("N must be at least 1")
continue
break
return (numberOfRows)
#Should generate Pascals triangle (shaped like a ladder)
def generate_pascal_triangle():
n = get_triangle_size()
if n == 1:
triangle = [[1]]
elif n == 2:
triangle = [[1], [1, 1]]
else:
triangle = [[1], [1, 1]]
for i in range(2, n):
row = [1]
for j in range(1, i):
row.append(triangle[i-1][j-1] + triangle[i-1][j])
row.append(1)
triangle.append(row)
#should convert the lists of pascal triangle into a string
def pascal_triangle_tostring():
output = ""
triangle = get_triangle_size()
for row in triangle:
for value in row:
output += str(value) + "\t"
output += '\n'
#Main code body should call these functions to print out pascal triangle
print("This program prints a Pascal's triangle with n line")
triangle_size = get_triangle_size()
pascal_triangle = generate_pascal_triangle()
print("Pascal’s triangle with %d lines:\n" % triangle_size)
print(pascal_triangle_tostring(pascal_triangle))
Upvotes: 1
Views: 151
Reputation: 6156
now I improved Your code so now it seems to work, so I know You have questions so please ask them. also You should really learn about function arguments
and f strings
in python
#should take user input and validates it
def get_triangle_size():
# while True:
numberOfRows = input("Enter the no. of rows: ")
try:
numberOfRows = int(numberOfRows)
except ValueError:
print("N must be an integer")
if numberOfRows <= 0:
print("N must be at least 1")
return numberOfRows
#Should generate Pascals triangle (shaped like a ladder)
def generate_pascal_triangle(n):
if n == 1:
triangle = [[1]]
elif n == 2:
triangle = [[1], [1, 1]]
else:
triangle = [[1], [1, 1]]
for i in range(2, n):
row = [1]
for j in range(1, i):
row.append(triangle[i-1][j-1] + triangle[i-1][j])
row.append(1)
triangle.append(row)
return triangle
#should convert the lists of pascal triangle into a string
def pascal_triangle_tostring(triangle):
output = ""
for row in triangle:
for value in row:
output += str(value) + "\t"
output += '\n'
return output
#Main code body should call these functions to print out pascal triangle
print("This program prints a Pascal's triangle with n line")
triangle_size = get_triangle_size()
pascal_triangle = generate_pascal_triangle(triangle_size)
print("Pascal’s triangle with %d lines:\n" % triangle_size)
print(pascal_triangle_tostring(pascal_triangle))
Upvotes: 2