I am having trouble trying to figure out how to write code so the room number increases by 1 each time it asks for length and width. Here's what the assignment is about..

In this program, you are going to calculate the total square footage of a five room apartment. (Yes, this is a straightforward count controlled loop problem)

You must use a loop to implement your solution. You can use either a for loop or a while loop.

Here is some sample output:

Please enter length and width of room 1: 10,20
Please enter length and width of room 2: 40,70
Please enter length and width of room 3: 25,40
Please enter length and width of room 4: 15,30
Please enter length and width of room 5: 50,30
The total square footage is 5950

Here is the code I came up with so far:

def main():
sum=0
count=0
x=0
for i in range (5):
    x=x+1
    l,w=eval(input("Please enter length and width of room"(x)))
    sum=sum+(l*w)
    count=count+1
print("The total square footage is", sum)

main()

I am a new programmer so any help would be appreciated. Thanks!

Recommended Answers

All 5 Replies

When I run the code, it results in this error

File "./test_1.py", line 110, in <module>
l,w=eval(input("Please enter length and width of room #"+str(x)))
TypeError: eval() arg 1 must be a string or code object

eval() is used to evaluate formulas or code so is not appropriate here and is generally considered bad practice as it will run anything that is entered i.e someone can enter a command to delete all of your files if they want to. Get the program to run first and then work on the room number problem. If you are using Python2.X (you didn't say) then input returns an integer so you can't separate the entries as that requires entering a non-integer character like a comma.

Formatting and concatenating strings http://www.diveintopython.net/native_data_types/formatting_strings.html
A Python2.x example

for i in range (5):
    x=x+1
    entry=raw_input("Please enter length and width of room ")
    l,w = entry.split(",")
    sum=sum+int(l)*int(w) 

I'm sorry I forgot to include the version. I use Python 3.2.2. Is there a big difference between that and Python 2?I will check out the link tonight when I get home and see if I can figure it out. Thank you for your help.

You can use woooee's code, just replace raw_input with input

Now I am attempting to make the room numbers count upward but I keep getting an error message saying:
area=input("Enter length and width of room",(roomNumber))
TypeError: input expected at most 1 arguments, got 2

def main():
    sum=0
    count=0
    roomNumber=0
    for i in range(1,6,1):
        roomNumber=roomNumber+1
        area=input("Enter length and width of room",(roomNumber))
        l,w = area.split(",")
        sum=sum+int(l)*int(w)
    print(sum)

main()

Nevermind the last post. I figured it out. Thanks everybody!!!!!

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

def main():
    sum=0
    count=0
    roomNumber=0
    for i in range(1,6,1):
        roomNumber=roomNumber+1
        area=input("Enter length and width of room"+str(roomNumber))
        l,w = area.split(",")
        sum=sum+int(l)*int(w)
    print(sum)

main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.