how to create a new variable in a loop
<p>I'm trying to make a function that prints n number of rows of the following sequence  </p>

<pre><code>1  
2 3  
4 5 6  
7 8 9 10  
11 12 13 14 15  
...
</code></pre>

<p>this is what I have so far:</p>

<pre><code>def numTriangle(n):  
    #n = number of rows  
    integers = range(0,n-1)  
    val = 1  
    places = 1  
    for num in integers:  
        print val   
        val = val + 1  
</code></pre>

<p>I've realized that I need to make a loop in which it prints the value, then adds one, but also creates a new variable that is one greater than the previous and prints the original value + 1 and the new variable and so on. How would I create a new variable for each line, or are there any other alternatives to this function, such as printing an additional part of the index for each row?</p>

<p>Not trying to do your homework for you, but you at least have a basic description of your plan. Here is a pretty minimal version.</p>

<pre><code>def pyramid(n):
    k = 1
    for i in range(n):
        print ' '.join(map(str, range(k, k+i+1)))
        k += i + 1
</code></pre>

<p>Here is a version that is more verbose.</p>

<pre><code>def pyramid(n):
    k = 1
    for i in range(1, n +1):
        first_number = k
        next_row_first_number = k + i
        numbers = range(first_number, next_row_first_number)
        numbers_as_strings = map(str, numbers)
        line = ' '.join(numbers_as_strings)
        print line
        k = next_row_first_number
</code></pre>

<p>And multiple loops!</p>

<pre><code>def pyramid(n):
    k = 1
    for i in range(1, n +1):
        first_number = k
        next_row_first_number = k + i
        numbers = range(first_number, next_row_first_number)
        line = ""
        for num in numbers:
            line = line + str(num) + ' '
        print line
        k = next_row_first_number
</code></pre>

python jes