How do I use a while loop to add a variable every time it loops?
<p>I'm writing a program that tells the user to input the amount of rooms that a property has, and then a while loop finds out the width and length of each room. I feel like I need the while loop to create two extra variables to store the width and length every time it iterates, however I can not figure out how.
here is my code so far:</p>

<pre><code>roomCount = 1
print("Please answer the questions to find the floor size.")
rooms = int(input("How many rooms has the property got?:\n"))
while roomcount &gt;= rooms:
    print("For room", roomcount,", what is the length?:\n")
</code></pre>

<p>Its not much, but I have been searching the internet and haven't found out how.</p>

<p>What I would like the program to do is to ask the user how many rooms the property has and then for each room it should ask for the width and length of the room. The program then should display the total area of the floor space in a user friendly format</p>

<p>updated code:</p>

<pre><code>currentRoomNumber = 0
currentRoomNumber2 = 0
floorspace = 0
whileLoop = 0
print("Please answer the questions to find the floor size.")
numberOfRooms = int(input("How many rooms has the property got?: "))
roomWidths= list()
roomLengths = list()
while currentRoomNumber &lt; numberOfRooms:
    roomWidths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the width?: ")))
    roomLengths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the length?: ")))
    currentRoomNumber += 1

while whileLoop &lt; numberOfRooms:
    floorspace += (roomLengths[(currentRoomNumer2)] * roomWidths[(currentRoomNumber2)])
    currentRoomNumber2 += 1
    whileLoop += 1

print(floorspace)
</code></pre>

<p>However, after inputting the values of the room dimensions, it gives me a traceback error on line 15 and says <code>currentRoomNumber2</code> is not defined. Where have I gone wrong?</p>

<p>From your question it seems like this is what you want:</p>

<pre><code>    print("Please answer the questions to find the floor size.")
    numberOfRooms = int(input("How many rooms has the property got?: "))
    currentRoomNumber = 0
    roomLengths = list()
    while currentRoomNumber &lt; numberOfRooms:
        roomLengths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the length?: ")))
        currentRoomNumber += 1
    print roomLengths
</code></pre>

<p>This puts each room's length into a list (keep in mind room "1" according to the user is room "0" to you).</p>

<p>When you run this, it looks like this (I put the length of each room as whatever the room number was):</p>

<pre><code>    Please answer the questions to find the floor size.                                                                                                                                                  
    How many rooms has the property got?: 5                                                                                                                                                              
    For room 1, what is the length?: 1                                                                                                                                                                   
    For room 2, what is the length?: 2                                                                                                                                                                   
    For room 3, what is the length?: 3                                                                                                                                                                   
    For room 4, what is the length?: 4                                                                                                                                                                   
    For room 5, what is the length?: 5                                                                                                                                                                   
    [1, 2, 3, 4, 5] 
</code></pre>

<p>To access the lengths of each room, like I said before, make sure you reference room "1" (length 1) as room "0", meaning you will reference it as:</p>

<pre><code>    print roomLengths[0]
</code></pre>

<p>This may be simple, but I wanted to make it clear to you since you were asking how to "create variables", really what you want is a list since you don't know how many "variables" you will want to create; so this list can have however many room lengths you need.</p>

<p>To add the width in there, you would just add another list and input like so:</p>

<pre><code>    print("Please answer the questions to find the floor size.")
    numberOfRooms = int(input("How many rooms has the property got?: "))
    currentRoomNumber = 0
    roomWidths= list()
    roomLengths = list()
    while currentRoomNumber &lt; numberOfRooms:
        roomWidths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the width?: ")))
        roomLengths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the length?: ")))
        currentRoomNumber += 1
    print "Room widths:"
    print roomWidths
    print "Room lengths:"
    print roomLengths
</code></pre>

<p>The output/running of the script would then be something like this:</p>

<pre><code>    Please answer the questions to find the floor size.                                                                                                                                                  
    How many rooms has the property got?: 3                                                                                                                                                              
    For room 1, what is the width?: 1                                                                                                                                                                    
    For room 1, what is the length?: 2                                                                                                                                                                   
    For room 2, what is the width?: 3                                                                                                                                                                    
    For room 2, what is the length?: 4                                                                                                                                                                   
    For room 3, what is the width?: 5                                                                                                                                                                    
    For room 3, what is the length?: 6                                                                                                                                                                   
    Room widths:                                                                                                                                                                                         
    [1, 3, 5]                                                                                                                                                                                            
    Room lengths:                                                                                                                                                                                        
    [2, 4, 6]  
</code></pre>

python variables while-loop