Quick actions

cmd+k|ctrl+k

Navigation

Languages

21-May-2026 Lesson 10 (Additional 2)(P.93)-REVTONY

Snippet info

Language

Python

Visibility

public

Author

michaelluiwaihung

Created

2026-05-27T03:15:48.647485Z

Updated

2026-05-28T02:37:00.662303Z

while True:
    while True:
        W= input("Please input your weight in kg")
        H= input("Please input your height in cm")
    
        try:
            W= float(W)
            H=float(H)/100
            #  Exit the loop because inputs are successful!
            break
            
        except ValueError:
            print("please input a number!")
    bmi = W / (H**2)
    bmi = round(bmi,2)
    if bmi <18.5:
        print (f"Your BMI is: {bmi}, you're underweitghted")
    elif 18.5 <=bmi<23:
        print (f"Your BMI is: {bmi}, you're fit")
    elif 23 <=bmi <=25:
        print(f"Your BMI is: {bmi}, you're overweighted!")
    else:
        print(f"Your BMI is: {bmi}, you're obese!")
    print('-'*40)
    # NEW SECURE EXIT LOGIC:
    while True:
        quit_choice = input("Do you want to calculate another BMI? (y/n): ").strip().lower()
        
        if quit_choice == 'n':
            print("Thank you for using the BMI calculator. Goodbye!")
            # This breaks out of the OUTER loop to close the program
            break 
        elif quit_choice == 'y':
            print("Starting next calculation...\n")
            # This breaks out of the quit loop so the program can loop back to the top
            break 
        else:
            # If they typed anything else, the loop repeats and asks again
            print("Invalid input! Please type exactly 'y' for Yes or 'n' for No.")
            
    # This double-check ensures that if they chose 'n', the outer loop actually ends
    if quit_choice == 'n':
        break
INFO