BMI 计算器不输出 Python

时间:2021-01-21 23:10:49

标签: python calculation

我正在用 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()

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

1 个答案:

答案 0 :(得分:1)

您应该更改检查输入的 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()
相关问题