0

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?

6
  • 3
    room("For room", roomcount,", what is the length?:\n") Is this a function call because it sure looks like one. What are you trying to do here? Commented Sep 14, 2016 at 19:25
  • It's my brain not working properly, sorry. Its meant to say print Commented Sep 14, 2016 at 19:26
  • Are you trying to increase roomcount each time your while loops? if so just go roomcount += 1 Commented Sep 14, 2016 at 19:27
  • You also have two different variables there, roomCount and roomcount, and I think your while expression should look like roomcount <= rooms. Also, don't forget to increment roomcount after each iteration. Commented Sep 14, 2016 at 19:27
  • 2
    Give us a test case and also clearly explain what your want. Commented Sep 14, 2016 at 19:27

4 Answers 4

1

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]  
Sign up to request clarification or add additional context in comments.

4 Comments

How would I times the rooms length by width for each room? floorArea = roomWidths[0] * roomLengths[0] or anything like that?
Exactly like that.
@PavelGurkov I know that that's how I would check it now, but how would i get it to check every room?
@Student You'd need to iterate over every roomWidths and corresponding roomLengths, like this: for i in range(len(roomWidths)): floorArea = roomWidths[i] * roomLengths[i] or for index, width in enumerate(roomWidths): floorArea = width * roomLengths[index]
0

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.

Comments

0

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)

Comments

0

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.

1 Comment

I agree with you, but I am only in Secondary school and have not done any coding at all for the past 6 weeks. Sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.