Search code examples
pythonpython-3.xinputpascals-triangle

Number Triangle using user input


Trying to create a number triangle using user input for each individual part of the triangle. I figured out how to put each row into a list and get user input for each individual number but I need help doing it without having each row in a list.

rows = int(dataLines) # --> convert user input to an integer
def triangle(rows):
    PrintingList = list()
    for rownum in range (1, rows + 1): # use colon after control structure to denote the beginning of block of code
        PrintingList.append([]) # append a row
        for iteration in range (rownum):
            newValue = input("Please enter the next number:")
            PrintingList[rownum - 1].append(int(newValue))
            print()

    for item in PrintingList:
      print (item)
triangle(rows)

This only got me lists for each row. Desired output would be something like

1

2 5

5 7 8

9 15 2 3

using user input for each individual number


Solution

  • Change This In Your Code:-

    for item in PrintingList:
      print (*item)
    

    Here we unpacked each element of the list using an List Unpacking Operator *. This got us rid of those brackets, in the beginning and end of each row.

    This Gives us Output:-

    1
    
    2 5
    
    5 7 8
    
    9 15 2 3