簡體   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