简体   繁体   中英

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. The code is 100% functional other than that. 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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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