We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,901 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Python Count Controlled Loop Question

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!

3
Contributors
5
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
10
Views
Question
Answered
Programmer_Girl
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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) 
woooee
Posting Maven
2,705 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

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.

Programmer_Girl
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7

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
Solved Threads: 0
Skill Endorsements: 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()
Programmer_Girl
Newbie Poster
4 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Ene Uran and woooee

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0778 seconds using 2.7MB