如何使我的程序循环回来并在Python中重新启动?

时间:2017-11-10 17:18:49

标签: python loops

我理解此问题之前已被问过,但我找不到合适的解决方案。我是Python的新手,需要创建一个允许用户输入多个BMI的BMI计算器,但我不能让它成功地循环回到开头。我尝试了两种方法而且都没有工作。

这是第一个我拥有的,但是这个不会像“继续不循环”那样运行: `

while True:
    print("BMI Calculator")

weight = float(input("\nPlease enter your weight in KG: "))
height = float(input("\nPlease enter your height in metres: "))
bmi = weight/(height*height)

if bmi <= 18.5:
    print("Your BMI is", bmi,"which means you are underweight.")

elif bmi > 18.5 and bmi < 25:
    print("Your BMI is: ", bmi, "which means you are normal")

elif bmi > 25 and bmi < 30:
    print("Your BMI is: ", bmi, "which means you are overweight")

elif bmi > 30:
    print("Your BMI is: ", bmi, "which means you are obese")

else:
    print("There was an error with your input, Sorry.")

while True:
    answer = input("Would you like to enter another? key y/n: ")
    if answer in ("y", "n"):
        break
    print("Invalid Input")
if answer == "y":
    continue
else:
    input("\nPress the enter key to exit")
    break

`

我拥有的另一个是这个,但它会打印BMI计算器,然后停止: `

def start():
    print("\nBMI Calculator")

weight = float(input("\nPlease enter your weight in KG: "))
height = float(input("\nPlease enter your height in metres: "))
bmi = weight/(height*height)

if bmi <= 18.5:
            print("Your BMI is", bmi,"which means you are underweight.")

elif bmi > 18.5 and bmi < 25:
            print("Your BMI is: ", bmi, "which means you are normal")

elif bmi > 25 and bmi < 30:
            print("Your BMI is: ", bmi, "which means you are overweight")

elif bmi > 30:
            print("Your BMI is: ", bmi, "which means you are obese")

else:
            print("There was an error with your input, Sorry.")

answer = input("Would you like to enter another? key y/n: ")
while answer == "y":
        start()
        answer = None

if answer == "n":
    input("\nPress the enter key to exit")

`

3 个答案:

答案 0 :(得分:4)

你的问题是关于压痕水平,劳拉。如果连续的行具有相同的缩进(不同于C或Java,其中块通过打开和关闭括号分隔),Python知道某些东西在代码块内。

您的代码应如下所示:

while True:
    print("BMI Calculator")

    weight = float(input("\nPlease enter your weight in KG: "))
    height = float(input("\nPlease enter your height in metres: "))
    bmi = weight/(height*height)

    if bmi <= 18.5:
        print("Your BMI is", bmi,"which means you are underweight.")

    elif bmi > 18.5 and bmi < 25:
        print("Your BMI is: ", bmi, "which means you are normal")

    elif bmi > 25 and bmi < 30:
        print("Your BMI is: ", bmi, "which means you are overweight")

    elif bmi > 30:
        print("Your BMI is: ", bmi, "which means you are obese")

    else:
        print("There was an error with your input, Sorry.")

    answer = input("Would you like to enter another? key y/n: ")

    if answer not in ("y", "n"):
        print("Invalid Input")
        break

    if answer == "y":
        continue
    else:
        input("\nPress the enter key to exit")
        break

在这段代码中,我改变了布尔测试和指令顺序:

if answer in ("y", "n"):
    break
print("Invalid Input")

if answer not in ("y", "n"):
    print("Invalid Input")
    break

如果您从循环中断,则不会执行该循环中的以下代码行。此外,您正在进行的比较将始终返回True,因为答案将 (“y”,“n”)。我也删除了最后一个循环因为它更有意义。

对于第二个代码,函数start()正在做的唯一事情是print("\nBMI Calculator"),同样是因为缩进级别。

希望有所帮助:)

答案 1 :(得分:0)

在第二个例子中,您需要在while循环中添加yes no question。这样的事情应该有效。

def start():
    print("\nBMI Calculator")

    while True:
        weight = float(input("\nPlease enter your weight in KG: "))
        height = float(input("\nPlease enter your height in metres: "))
        bmi = weight/(height*height)

        if bmi <= 18.5:
            print("Your BMI is", bmi,"which means you are underweight.")
        elif bmi > 18.5 and bmi < 25:
            print("Your BMI is: ", bmi, "which means you are normal")
        elif bmi > 25 and bmi < 30:
            print("Your BMI is: ", bmi, "which means you are overweight")
        elif bmi > 30:
            print("Your BMI is: ", bmi, "which means you are obese")
        else:
            print("There was an error with your input, Sorry.")
        answer = input("Would you like to enter another? key y/n: ")
        while answer == "y":
            start()
            answer = None
        if answer == "n":
            exit()

start()

我没有在y / n问题中添加else语句,但这应该是一个简单的修复,看起来你之前写过的语句可以正常工作。

答案 2 :(得分:0)

第一件事:

  

您正面临缩进的问题原因:

第二件事:

对于第二个功能,我有一些建议:

而不是print使用return

  

功能一般是接受输入和返回   一些东西。 return语句使您的函数退出并处理   将值返回给调用者。

并使用python .format()方法

这是您的第二个代码,经过了一些修改:

def start():
    print("\nBMI Calculator")
    weight = float(input("\nPlease enter your weight in KG: "))
    height = float(input("\nPlease enter your height in metres: "))
    bmi = weight/(height*height)
    if bmi <= 18.5:
        return "Your BMI is : {} which means you are underweight".format(bmi)
    elif bmi > 18.5 and bmi < 25:
        return "Your BMI is : {} which means you are normal".format(bmi)
    elif bmi > 25 and bmi < 30:
        return "Your BMI is : {} which means you are overweight".format(bmi)
    elif bmi > 30:
        return "Your BMI is : {} which means you are obese".format(bmi)
    else:
        return "There was an error with your input, Sorry."

print(start())

answer = input("Would you like to enter another? key y/n: ")
if answer == "y":
    start()
elif answer == "n":
    input("\nPress the enter key to exit")