简体   繁体   English

BMI计算器不输出Python

[英]BMI Calculator not outputting Python

I'm building a BMI Calculator in Python and after choosing the metric or imperial system it won't post.我正在 Python 中构建一个 BMI 计算器,在选择公制或英制后它不会发布。 The code is 100% functional other than that.代码除此之外是 100% 的功能。 I added the option to choose if you want to use the imperial system or the metric system.我添加了选项来选择您是要使用英制还是公制。 How could I improve the code?我该如何改进代码?

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

I'm supposed to add more details, but I don't really have any more details, to be honest, so now I'm just writing all of this just so I can post this我应该添加更多细节,但老实说,我真的没有更多细节,所以现在我只是写下所有这些,以便我可以发布这个

You should change the while loop where you check the inputs.您应该更改检查输入的 while 循环。 The code below lowercases the input and checks whether it is "metric" or "imperial", so there is no need to check for capitalized parameters下面的代码将输入小写并检查它是“公制”还是“英制”,因此无需检查大写参数

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