Home > Back-end >  Program does not rise predefined ZeroDivisionError
Program does not rise predefined ZeroDivisionError

Time:11-14

I am building a simple BMI calculator program and I wish it to take users back to the input field if they try zero division. i defined what output should be at that situation but it still prints out ZeroDivisionError

def bmi_calculator():
    user_height = float(input('Enter your height in cm: '))  # user height in cm
    user_weight = float(input('Enter your weight in kg: '))  # user weight in kb
    bmi = user_weight / (user_height / 100) ** 2  # BMI formula
    while user_height <= 0 or user_weight <= 0:
        try:
            print("your weight or height can't be below or equal 0 \n enter your credentials again... ")
            user_height = float(input('Enter your height in cm: '))
            user_weight = float(input('Enter your weight in kg: '))
        except ZeroDivisionError:
            print("You can't divide by 0")
            user_height = float(input('Enter your height in cm: '))  # user height in cm
            user_weight = float(input('Enter your weight in kg: '))  # user weight in kb
    print(f'Your BMI is: {bmi}')

print(bmi_calculator())

CodePudding user response:

I have already specified the problem in your code here, additionly:-

You are asking for the input from the user every time uneccesarily here's a short answer:

def bmi_calculator():
    while True:
        try:
            error="your weight or height can't be below or equal 0"
            height = float(input('Enter your height in cm: '))
            weight = float(input('Enter your weight: '))
            bmi=weight / (height / 100) ** 2
            if weight==0:
                print(error)
                continue
            print(f'Your BMI is {bmi}')
        except ZeroDivisionError:
            print(error)
bmi_calculator()

Note:- The BMI can be displayed in negative number which isn't correct I leave it to tackle that exception upon you. If you aren't able to solve that you can comment below this answer and I will update my answer accordingly.

CodePudding user response:

For the ZeroDivisionError exception to be caught, put the line of code for the division inside the try block.
Then, if the specified exception occurs, the except statement comes into action and prints,
You can't divide by 0.

CodePudding user response:

That's because you first make the division then start the while loop. This should be something like this:

def bmi_calculator():
    user_height = float(input('Enter your height in cm: '))  # user height in cm
    user_weight = float(input('Enter your weight in kg: '))  # user weight in kb

    while user_height <= 0 or user_weight <= 0:
        print("your weight or height can't be below or equal 0 \n enter your credentials again... ")
        user_height = float(input('Enter your height in cm: '))
        user_weight = float(input('Enter your weight in kg: '))

    bmi = user_weight / (user_height / 100) ** 2  # BMI formula
    print(f'Your BMI is: {bmi}')

bmi_calculator()

After all, I recommended using if statement in the while loop for more clarification.

  • Related