Problem Statement

Write a program to construct and print Pascal’s Triangle for a given number of rows. The user should be prompted to enter a positive integer indicating the number of rows, and if the user provides an invalid input the program should prompt for additional input until valid input is received.

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Handling Input

def main():

main()

Handling Input

def positive_input():
    x = int(input("Enter a positive integer: "))
    while x <= 0:
        print("Invalid input!")
        x = int(input("Enter a positive integer: "))
    return x

def main():
main()

Handling Input

def positive_input():
    x = int(input("Enter a positive integer: "))
    while x <= 0:
        print("Invalid input!")
        x = int(input("Enter a positive integer: "))
    return x

def main(): n = positive_input()
main()

Initial Setup

def main():
    n = positive_input()
    row = [1]
    print(row)

Initial Setup

def main():
    n = positive_input()
    row = [1]
    print(row)
    for i in range(1, n):
        # update row
        print(row)

Next Row

def update_row(row):

Next Row

def update_row(row):
    i = 0
    while i < len(row):
        # use list elements
        i = i + 1

Next Row

def update_row(row):
    i = 0
    new_row = [1]
    while i < len(row):
        # use list elements
        i = i + 1

Next Row

def update_row(row):
    i = 0
    new_row = [1]
    while i < len(row):
        new_row.append(row[i] + row[i + 1])
        i = i + 1

Next Row

def update_row(row):
    i = 0
    new_row = [1]
    while i < len(row):
        new_row.append(row[i] + row[i + 1])
        i = i + 1
    new_row.append(1)
    return new_row

Final Program

def positive_input():
    x = int(input("Enter a positive integer: "))
    while x <= 0:
        print("Invalid input!")
        x = int(input("Enter a positive integer: "))
    return x

def update_row(row): i = 0 new_row = [1] while i < len(row): new_row.append(row[i] + row[i + 1]) i = i + 1 new_row.append(1) return new_row
def main(): n = positive_input() row = [1] print(row) for i in range(1, n): row = update_row(row) print(row)
main()

Logic Error

Logic Error

def update_row(row):
    i = 0
    new_row = [1]
    while i < len(row):
        new_row.append(row[i] + row[i + 1])
        i = i + 1
    new_row.append(1)
    return new_row

Logic Error

def update_row(row):
    i = 0
    new_row = [1]
    while i < len(row) - 1:
        new_row.append(row[i] + row[i + 1])
        i = i + 1
    new_row.append(1)
    return new_row

Logic Error Fixed!

Final Program

def positive_input():
    x = int(input("Enter a positive integer: "))
    while x <= 0:
        print("Invalid input!")
        x = int(input("Enter a positive integer: "))
    return x

def update_row(row): i = 0 new_row = [1] while i < len(row) - 1: new_row.append(row[i] + row[i + 1]) i = i + 1 new_row.append(1) return new_row
def main(): n = positive_input() row = [1] print(row) for i in range(1, n): row = update_row(row) print(row)
main()