Student
Student

Reputation: 25

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

updated 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 < 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 < numberOfRooms:
    floorspace += (roomLengths[(currentRoomNumer2)] * roomWidths[(currentRoomNumber2)])
    currentRoomNumber2 += 1
    whileLoop += 1

print(floorspace)

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

Upvotes: 0

Views: 4415

Answers (4)

S. Jacob Powell
S. Jacob Powell

Reputation: 406

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]  

Upvotes: 1

Pavel Gurkov
Pavel Gurkov

Reputation: 757

I'd do it this way.

rooms_total = int(input('How many rooms?'))
rooms_info = list()

for i in range(rooms_total):
    length = int(input('Length for room #{}?'.format(i)))
    width = int(input('Width for room #{}?'.format(i)))
    rooms_info.append({'length': length, 'width': width})

space = sum([x['length'] * x['width'] for x in rooms_info])
print(space)

Class feels like an overkill (unless you specifically want to practice classes), and dictionary doesn't feel like a proper outer data structure here.

It's just my humble opinion, but you need to read more about loops and basic data structures in Python.

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191743

I'd recommend going for a Room class. Then you can define an area method on it.

class Room():
    def __init__(self, w, l):
        self.width = w
        self.length = l

    def area(self):
        return self.width * self.length

Then, for your input, store those into a list. Use a for-loop instead. No need for a while-loop.

print("Please answer the questions to find the floor size.")

roomCount = int(input("How many rooms does the property have?\n"))

rooms = []
for roomNum in range(roomCount):
    l = float(input("For room {} what is the length?\n".format(roomNum + 1)))
    w = float(input("For room {} what is the width?\n".format(roomNum + 1)))
    rooms.append(Room(w, l))

When you are done with that, you just loop over the objects and add up the areas.

size = sum(r.area() for r in rooms)
print(size)

Upvotes: 0

noteness
noteness

Reputation: 2520

Thinking you want the length and breadth to be saved by the room number, I would do something like this:

rooms = {}

print("Please answer the questions to find the floor size.")

rooms = int(input("How many rooms has the property got?:\n"))

for room_num in range(1, rooms+1):
    length = int(input("What is the lenth of room number {}: ".format(room_num)))
    width = int(input("What is the lwidth of room number {}: ".format(room_num)))
    rooms[room_num] = {'length': length, 'width': width}

Then to get the length of room 1, just look it up: rooms[1]['length'], width: rooms[1]['width'] ,etc. etc.

Upvotes: 0

Related Questions