Python-Need to turn this into a loop so it look like my picture?

Go To StackoverFlow.com

1

I need to write this program using a loop

http://imgur.com/CGRMn

this is what i have so far but my program has to look exactly and the picture???

def main():

    sum=0.0
    for i in range(1,6):
        x,y=eval(input("Please enter length and width of room:"))
        sf=(x*y)
        sum=sum+sf
        print("The total square footage is",sum)

main()

Thank you for your help...

2012-04-04 03:22
by maurico gomez
You've made a goo start, but you haven't actually asked a question here. What are you having trouble with? Please read the [FAQ] and [ask] for more details on how to post here - Jim Garrison 2012-04-04 03:26
"I need to write this program using a loop - fceruti 2012-04-04 03:26
I need to know how to get the room number in my input to change in the loop as in the pic, room 1 to - maurico gomez 2012-04-04 03:45


4

Just drop the indent on the print statement.

sum=0.0
for i in range(1,6):
    x,y=eval(input("Please enter length and width of room:"))
    sf=(x*y)
    sum=sum+sf
print("The total square footage is",sum)

edited:

This is a common technique in python you could use to achieve what you want:

sum=0.0
for i in range(1,6):
    x,y=eval(input("Please enter length and width of room %i:" % i ))
    sf=(x*y)
    sum=sum+sf
print("The total square footage is %i" % sum )

What i'm doing here is placing a wildcard in the middle of the string and then pass the parameters. '%i' tells the % operator that you are going to insert an integer. You can also put '%s' if you were going to add a string. There are a couple more you can check out. This is another example for the console:

>>> user_name = 'mauricio'
>>> sum = 42
>>> line_to_print = 'Hello user %s, your sum is %i' % (user_name, sum)
>>> print(line_to_print)
Hello mauricio, your sum is 42
2012-04-04 03:26
by fceruti
Thank you so much, one more thing do u know how to make my input question have the room number like in the pic it shows room 1 then room 2 an so on in the loop?? - maurico gomez 2012-04-04 03:35
I edited my comment to answer your new question - fceruti 2012-04-04 03:49
Oh, and your welcome : - fceruti 2012-04-04 03:49
Ads