Home > Net >  How do i write code to handle Input error with BMI calculator?
How do i write code to handle Input error with BMI calculator?

Time:11-17

Enter your height in meters: t Invalid choice. Try again Enter your height in meters: 1.7 Enter your weight in kg: g Invalid choice. Try again Enter your height in meters:

This is my output. The first time the user inputs an invalid choice the correct display is shown and the user is directed to re-enter their height. When the weight input is incorrect the code is incorrect and repeats enter your height rather than weight.

def mainMenu():
    print("1. Calculate body mass index (BMI).")
    print("2. View membership cost.")
    print("3. Exit the program.")
    while True:
        try:
            choice = int(input("Enter your choice:  "))
            if choice == 1:
                BMI()
                break
            elif choice ==2:
                Membership()
                break
            elif choice ==3:
                break
            else:
                print("Incorrect choice. Enter 1-3")
                mainMenu
        except ValueError:
            print("Invalid choice. Enter 1-3")
    exit

def BMI():
    while True:
        try:
            h=float(input("Enter your height in meters: "))
            w=float(input("Enter your weight in kg: "))
            BMI=w/(h*h)
            print("BMI Calculated is:  ",BMI)
               
            if(BMI<18.5):
                print("Underweight")
            if(BMI>=18.5 and BMI <25):
                print("Normal")
            if(BMI>=25 and BMI <30):
                print("Overweight")
            if(BMI>30):
                print("Obese")
            else:
                print("Incorrect choice.")
                mainMenu
        except ValueError:
            print("Invalid choice. Try again")
    exit


mainMenu()

I am new to coding so would appreciate any help.

CodePudding user response:

Like this:-

def BMI():

while True:

    try:

        try:

            h=float(input("Enter your height in meters: "))

        except ValueError:

            print("Invalid choice. Try again")

        try:  



            w=float(input("Enter your weight in kg: "))

        except ValueError:

            print("Invalid choice. Try again")

        
        BMI=w/(h*h)

        print("BMI Calculated is:  ",BMI)

           
        if(BMI<18.5):

            print("Underweight")

        if(BMI>=18.5 and BMI <25):

            print("Normal")

        if(BMI>=25 and BMI <30):

            print("Overweight")

        if(BMI>30):

            print("Obese")

        else:

            print("Incorrect choice.")

            mainMenu

    except ValueError:

        print("Invalid choice. Try again")

        
        
        

CodePudding user response:

See when the user enters the wrong weight then the whole code block will run again so to avoid this you can bind them in separate try and catch so for each height and weight it will check it individually.

Or

You can bind them with individual if else statements for checking their values.

CodePudding user response:

Yo here is another example, You can use.


def mainMenu():

    while True:
        print("1. Calculate body mass index (BMI).")
        print("2. View membership cost.")
        print("3. Exit the program.")
        try:
            choice = int(input("Enter your choice:  "))
            if choice == 1:
                BMI()
            elif choice ==2:
                # Membership()
                break
            elif choice ==3:
                break
            else:
                print("Incorrect choice. Enter 1-3")
                # here you tried to run the function mainMenu(). Don't do it while loop will take care of it.

        except ValueError:
            print("Invalid choice. Enter 1-3")

def BMI():
    # here i seperate the while loop for every input so it will only repeat the wrong input 
    while True:
        try:
            h=float(input("Enter your height in meters:  "))
            break
        except:
            print("Invalid height. ")

    while True:
        try:
            w=float(input("Enter your weight in kg: "))
            break
        except:
            print("Invalid Weight.")
        

    BMI=w/(h*h)


    print("\n\n") #this is here just to create some space between result and other program
    
    print("BMI Calculated is:  ",BMI)
        

    # you had used if statements for every one of these so i used elif instead
    # so the program won't have to read all these every time
    # it will be a little bit easier
    if(BMI<18.5):
        print("Underweight")
    elif(BMI>=18.5 and BMI <25):
        print("Normal")
    elif(BMI>=25 and BMI <30):
        print("Overweight")
    elif(BMI>30):
        print("Obese")
    else:
        print("incorrect choice")

    # here are these print statements to create some space when the program is run another time
    print("\n")
    print("=========================================")
    print("\n")


    mainMenu()


mainMenu()

and by the way i tested your program on myself. It told me i was overweight. I thought i was not that fat. 'EMOTIONAL DAMAGE'

  • Related