title: Trinomial triangle (Python)
body: <p>As part of my interest in learning Python, I've hit a stop when coming across an exercise that states:</p>
  
  <blockquote>
    <p>Consider the expression (1 + x + x^2)^n and write a program which
    calculates a modified Pascal’s triangle (known as the trinomial
    triangle) for the coefficients of its expansion. Can you come up with
    a simple rule (and prove that it works!) which would enable you to
    write down the coefficients of this triangle?</p>
  </blockquote>
  
  <p>So, I'm trying to write a code that prints out the <a href="http://mathworld.wolfram.com/TrinomialTriangle.html" rel="nofollow noreferrer">trinomial triangle</a> from a user input. This is the code I have so far:</p>
  
  <pre><code>import math
  
  rows = 0 #We count how many rows we print
  
  #We define a function that will calculate the triangle.
  def calc(n, r):
      if r == 0 or r == n:
          return 1
      return int(math.pow(1 + r + math.pow(r, 2), n))
  
  #We define a function that append the values in an array.
  def triangle(rows):
      result = [] #We need an array to collect our results.
      for count in range(rows): #For each count in the inputted rows
          row = [] #We need a row element to collect the rows.
          for element in range(count + 1):
              #We add the function calculation to the row array.
              row.append(calc(count, element))
          #We add the row(s) to the result array.
          result.append(row)
      return result
  
  number = int(input("How many rows do you want printed? "))
  
  #We can now print the results:
  for row in triangle(number):
      rows += 1 #We add one count to the amount of rows
      print("Row %d: %s" % (rows, row)) #Print everything
  </code></pre>
  
  <p>which returns</p>
  
  <pre><code>How many rows do you want printed? 5
  Row 1: [1]
  Row 2: [1, 1]
  Row 3: [1, 9, 1]
  Row 4: [1, 27, 343, 1]
  Row 5: [1, 81, 2401, 28561, 1]
  </code></pre>
  
  <p>And as I understand it, the expected result should be:</p>
  
  <pre><code>1
  1   1   1
  1   2   3   2   1
  1   3   6   7   6   3   1
  1   4   10  16  19  16  10  4  1
  </code></pre>
  
  <p>I don't exactly know how to proceed from here. Any suggestions to point me in the right direction would be appreciated.</p>