Quick actions

cmd+k|ctrl+k

Navigation

Languages

ERB 28 Jan 2026 Exercise BMI

Snippet info

Language

Python

Visibility

public

Author

michael-wong

Created

2026-01-29T14:21:05.249577Z

Updated

2026-01-29T14:21:05.249577Z

while True:
    try:
        weight = float(input("Please input your weight (kg): "))
        height = float(input("Please input your height (cm): "))

        # 合理範圍檢查
        if weight < 40 or weight > 1000:
            print("Invalid weight! Please enter a value between 40 and 1000 kg.\n")
            continue
        if height < 50 or height > 300:
            print("Invalid height! Please enter a value between 50 and 300 cm.\n")
            continue

    except ValueError:
        print("Invalid input! Please enter numbers only.\n")
        continue

    # 計算BMI
    BMI = round(weight / ((height / 100) ** 2), 2)

    # 判斷範圍
    if BMI < 18.5:
        print("Your BMI:", BMI, ", you are under-weighted!!!")
    elif BMI >= 18.5 and BMI < 23:
        print("Your BMI:", BMI, ", you are in normal range!!!")
    elif BMI >= 23 and BMI < 25:
        print("Your BMI:", BMI, ", you are over-weighted")
    else:
        print("Your BMI:", BMI, ", you are obese")

    # 是否繼續
    while True:
        choice = input("\nDo you want to calculate again? (Y/N): ").lower()
        if choice in ["y", "yes"]:
            break   # 跳出內層迴圈,繼續外層 while True
        elif choice in ["n", "no"]:
            print("Program ended. Goodbye!")
            exit()  # 結束整個程式
        else:
            print("Invalid choice! Please enter Y or N.")
INFO