Chris
Chris

Reputation: 1

Can't seem to get my program to loop correctly

Floor tile cost

print("Hello this program will help you figure out the cost to tile your floor")

x = input("Please enter the length of the floor that's being tiled: ")

y = input("Next, please enter the width of the floor: ")

fltype = raw_input("Now enter a shape that best describes the room you are getting tiled: ")
fltype = fltype.lower()

if fltype is "circle" or "ellipse":
    formula = ((x * y)*.5) * 3.14

elif fltype is "rectangle" or "square":
    formula = x * y

elif fltype is "triangle":
    formula = (x * .5) * y

else:
    print("Sorry unrecognized floor type please contact admin to add shape or try again.")

Where I would like the program to loop if the floor type is unrecognizable I know a while would work but I can't seem to get it.

tuc = input("What is the cost per unit of the tile you'd like: ")

pobs = input("Please Enter 1 if you are tiling the floor by yourself, otherwise enter 2 for professional flooring: ")

if pobs == 1:
    total = tuc * formula

elif pobs == 2:
    labor = input("What is the contractor's hourly labor cost ")
    total = (tuc * formula) + ((formula / 20) * labor)
else:
    print("Invalid command please try again ")

Also would be cool to loop this part if they didn't do pobs correctly

print("The total cost of the tiling project is $" + str(total))
print("Thank you for using my program any feedback is appreciated!")

Any feedback is good feedback hopefully I followed all rules thank everyone in advance.

Upvotes: 0

Views: 63

Answers (2)

Luc DUZAN
Luc DUZAN

Reputation: 1319

If I was you I will put your all program in a function that you can recall after when you want to replay the program. For example :

def myprog():
    print("Hello this program will help you figure out the cost to tile your floor")

    x = input("Please enter the length of the floor that's being tiled: ")

    y = input("Next, please enter the width of the floor: ")

    fltype = raw_input("Now enter a shape that best describes the room you are getting tiled: ")
    fltype = fltype.lower()

    if fltype in ["circle","ellipse"]:
        formula = ((x * y)*.5) * 3.14

    elif fltype in ["rectangle" , "square"]:
        formula = x * y

    elif fltype == "triangle":
        formula = (x * .5) * y

    else:
        print("Sorry unrecognized floor type please contact admin to add shape or try again.")
        myprog()
        return

    print("Your cost is %d" % (formula))

if __name__ == '__main__':
    #only call if you file is executed but not if your file is imported
    myprog()

I also correct your mistake in your if, in fact

fltype is "circle" or "ellipse" == (fltype is "circle") or "ellipse" == True #it always true

Something cleaner you be to repeat only the code that ask for the shape :

print("Hello this program will help you figure out the cost to tile your floor")

x = input("Please enter the length of the floor that's being tiled: ")

y = input("Next, please enter the width of the floor: ")

formula = None
while not formula :
    fltype = raw_input("Now enter a shape that best describes the room you are getting tiled: ")
    fltype = fltype.lower()

    if fltype in ["circle","ellipse"]:
        formula = ((x * y)*.5) * 3.14

    elif fltype in ["rectangle" , "square"]:
        formula = x * y

    elif fltype == "triangle":
        formula = (x * .5) * y

    else:
        print("Sorry unrecognized floor type please contact admin to add shape or try again.")

print("Your cost is %d" % (formula))

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304473

You have a few problems there:

if fltype is "circle" or "ellipse":

means

if (fltype is "circle") or "ellipse":

which will always be True. This might be why your attempt at the while loop failed

while True
    fltype = raw_input("Now enter a shape that best describes the room you are getting tiled: ")      
    fltype = fltype.lower()

    if fltype in {"circle", "ellipse"}:
        formula = ((x * y) * .25) * 3.14

    elif fltype in {"rectangle", "square"}:
        formula = x * y

    elif fltype == "triangle":
        formula = (x * .5) * y

    else:
        print("Sorry unrecognized floor type please contact admin to add shape or try again.")
        continue
    break

Upvotes: 1

Related Questions