python程序如何循环_如何让程序在Python中循环并重新启动?

劳拉,你的问题都是关于压痕的。Python知道如果连续的代码行都有相同的缩进(与C或Java不同,在C或Java中,代码块由左括号和右括号分隔)。在

您的代码应该如下所示: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

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

^{pr2}$

到if answer not in ("y", "n"):

print("Invalid Input")

break

如果中断循环,则不会执行该循环中的以下代码行。另外,您所做的比较总是返回True,因为答案是在(“y”,“n”)。我还删除了最后一个循环,因为这样更有意义。在

至于第二个代码,函数start()所做的唯一事情就是print("\nBMI Calculator"),这也是因为缩进级别的原因。在

希望有帮助:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值