繁体   English   中英

BMI计算器不输出Python

[英]BMI Calculator not outputting Python

我正在 Python 中构建一个 BMI 计算器,在选择公制或英制后它不会发布。 代码除此之外是 100% 的功能。 我添加了选项来选择您是要使用英制还是公制。 我该如何改进代码?

def WeightCalMetric() :

    print("BMI-Calculator")

    while True:
        try:
            UserHeight = float(input("What's your height in meters? "))
            break
        except:
            print("Your height has to be a number")

    while True:
        try:
            UserWeight = float(input("What's your weight in Kg? "))
            break
        except:
            print("Your weight has to be a number")

    Bmi = UserWeight / (UserHeight ** 2)
    FloatBmi = float("{0:.2f}".format(Bmi))

    if FloatBmi <= 18.5:
        print('Your BMI is', str(FloatBmi),'which means you are underweight.')

    elif FloatBmi > 18.5 and FloatBmi < 25:
        print('Your BMI is', str(FloatBmi),'which means you are a healthy weight.')

    elif FloatBmi > 25 and FloatBmi < 30:
        print('your BMI is', str(FloatBmi),'which means you are overweight.')

    elif FloatBmi > 30:
        print('Your BMI is', str(FloatBmi),'which means you are obese.')

def WeightCalImperial() :

    print("BMI-Calculator")

    while True:
        try:
            UserHeight = float(input("What's your height in inches? "))
            break
        except:
            print("Your height has to be a number")

    while True:
        try:
            UserWeight = float(input("What's your weight in Lbs? "))
            break
        except:
            print("Your weight has to be a number")

    Bmi = 703 * (UserWeight / (UserHeight ** 2))
    FloatBmi = float("{0:.2f}".format(Bmi))

    if FloatBmi <= 18.5:
        print('Your BMI is', str(FloatBmi),'which means you are underweight.')

    elif FloatBmi > 18.5 and FloatBmi < 25:
        print('Your BMI is', str(FloatBmi),'which means you are a healthy weight.')

    elif FloatBmi > 25 and FloatBmi < 30:
        print('your BMI is', str(FloatBmi),'which means you are overweight.')

    elif FloatBmi > 30:
        print('Your BMI is', str(FloatBmi),'which means you are obese.')

print("Hi welcome to this BMI Calculator")
print("First choose if you want to use the metric system or the imperial system")
print('Write "Metric" for the metric system or write "Imperial" for the imperial system')

KgOrLbs = None
while KgOrLbs not in ("metric", "Metric", "imperial", "Imperial"):
    KgOrLbs = input("Metric or Imperial? ")
    if KgOrLbs == "metric, Metric":
        WeightCalMetric()
    elif KgOrLbs == "imperial" "Imperial":
        WeightCalImperial()

我应该添加更多细节,但老实说,我真的没有更多细节,所以现在我只是写下所有这些,以便我可以发布这个

您应该更改检查输入的 while 循环。 下面的代码将输入小写并检查它是“公制”还是“英制”,因此无需检查大写参数

KgOrLbs = input("Metric or Imperial? ")
while KgOrLbs.lower() not in ["metric", "imperial"]:
    KgOrLbs = input("Metric or Imperial? ")  
    if KgOrLbs.lower() == "metric":
        WeightCalMetric()
    elif KgOrLbs.lower() == "imperial":
        WeightCalImperial()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM