1,105,450 Community Members

Python Count Controlled Loop Question

Programmer_Girl
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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!

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

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) 
Programmer_Girl
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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.

Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
0
 

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

Programmer_Girl
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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()
Programmer_Girl
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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()
Question Answered as of 2 Years Ago by Ene Uran and woooee
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: