Student Student - 2 months ago 8
Python Question

How do I use a while loop to add a variable every time it loops?

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:

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 >= rooms:
print("For room", roomcount,", what is the length?:\n")


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

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

Answer

From your question it seems like this is what you want:

    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 < numberOfRooms:
        roomLengths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the length?: ")))
        currentRoomNumber += 1
    print roomLengths

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

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

    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] 

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:

    print roomLengths[0]

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.

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

    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 < 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

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

    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]  
Comments