1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python: printing 1 row of Pascal Triangle

Discussão em 'Python' iniciado por Stack, Novembro 21, 2025.

  1. Stack

    Stack Membro Participativo

    New to learning python and am having some trouble understanding a solution provided? It has to do with Pascal Triangle and printing the rows when asking a user to "enter a row number"

    There were bits of the solution provided and the rest I fit in (first for loop)..

    n=int(input("Enter a row number: "))
    a=[]
    for i in range(n):
    a.append([])
    a.append(1)
    for j in range(1,i):
    a.append(a[i-1][j-1]+a[i-1][j])
    if(n!=0):
    a.append(1)
    for i in range(n):
    print(" "*(n-i),end=" ",sep=" ")
    for j in range(0,i+1):
    print('{:4}'.format(a[j]),end=" ")
    print()


    My question is which part of the code is printing the triangle structure? I assume the last for loop? Also if I wanted to just print 1 row, what would I be changing? EX: Input: 5 and output would be [1 4 6 4 1 ]

    Thank you and any help/advice would be appreciated

    Continue reading...

Compartilhe esta Página