Storing updated variables in a loop?
<p>I have written this program which asks the user about how many rectangles they wants to print out. It also asks for the width and height of each, and prints the triangles. After asking the height and width of each, it moves onto the next rectangle and so on. </p>

<p>This all works fine using the program I've made but at the end I want to print out the total area of all the rectangles that the user has created. How can I update my code and achieve this? How is it possible to store the area of the first rectangle and add the area of the second to this first area and so on?
Here is the code:</p>

<pre><code>size = input("How many rectangles?" ) #asks the number of rectangles 
i=1
n = 1
while i &lt;= size:
    w = input("Width "+str(n)+"? ") #asks for width of each rectangle
    h = input("Height "+str(n)+"? ") #asks for height of each rectangle
    n=n+1
    h1=1
    w1=1
    z = ""
    while w1 &lt;= w:
        z=z+"*"
        w1+=1
    while h1&lt;=h:
        print z
        h1+=1
    i+=1
</code></pre>

<p>How about you just accumulate the total area?</p>

<p>Above your loop, do:</p>

<pre><code>area = 0
</code></pre>

<p>Then, somewhere inside your loop, after you've got <code>w</code> and <code>h</code> from the user, just do</p>

<pre><code>area += w * h
</code></pre>

<p>When you finish looping, <code>area</code> will contain the total area.</p>

python python-2.7