似乎无法使我的程序正确循环

时间:2015-04-16 06:07:19

标签: python while-loop

地砖成本

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.")

如果地板类型无法识别,我希望程序循环,我知道有一段时间会工作,但我似乎无法得到它。

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 ")

如果他们没有正确地做pobs,那么循环这部分也会很酷

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

任何反馈意见都是好的反馈,希望我能提前遵守所有规则。

2 个答案:

答案 0 :(得分:1)

你有一些问题:

if fltype is "circle" or "ellipse":

装置

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

始终为True。这可能是您尝试while循环失败的原因

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

答案 1 :(得分:0)

如果我是你,我会把你的所有程序放在一个你想要重播程序后可以回忆的功能中。例如:

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()

我也纠正了你的错误,实际上是

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

更清洁一点,你只需重复要求形状的代码:

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))