BMI calculator


#1

Traceback (most recent call last):
File “C:\Users\artea\AppData\Local\Programs\Python\Python36-32\cophw3idle.py”, line 37, in
body_mass_index = (get_weight * 703) / (get_height ** 2)
ZeroDivisionError: float division by zero

I need help solving this error.
Any tips or guidance would be greatly appreciated.


#2

get_weight seems to be zero, zero / any number results in an error. Please copy paste your code so we can run it, debugs a lot easier

get_weight * 703 is still zero


#3

I thought your program looked wrong from the screenshot, but just to be sure I wrote it out and ran it.

Running your code as it is, I get the following output from Python:

Please do not use zero as a number!
Traceback (most recent call last):
  File "bmicalc.py", line 28, in <module>
    body_mass_index = (get_weight * 703) / (get_height ** 2)
ZeroDivisionError: float division by zero

The reason that we’re getting this specific error in the traceback is because you’ve declared your variables, get_height and get_weight and assigned them the value of 0.0 in both cases.

Immediately after assigning zero values to your variables, you’re attempting to calculate the BMI using them within your try statement. This throws the expected ZeroDivisionError which is caught by the except statement, and we get the right output to the console of “Please do not use zero as a number!”.

We handled the exception, so Python carries on.

There’s then two more functions defined, bmi_intro() and get_users_info(), but the next line that Python runs is here:

#user will enter their information above and we will then calculate.
body_mass_index = (get_weight * 703) / (get_height ** 2)           # this is the next line to run!

We still haven’t assigned either of the get_height or get_weight variables a value which is non-zero, but this time our divide-by-zero calculation isn’t sat nicely in a try statement so Python bails and throws your error at you.

Hopefully that make sense and you can see why you’re getting that particular error in this case.

There are a couple of other things in your code that will keep it from working, but I’m going to keep this post just covering the specific error you asked about. :slight_smile:


#4
def main():
    bmi_intro()
    get_users_info()

#Define Variables #Floats
get_weight = 0.0
get_height = 0.0
body_mass_index = 0.0


try:
    body_mass_index = (get_weight * 703) / (get_height ** 2)
except ZeroDivisionError:
    print("Please do not use zero as a number!")

#We will start with the introduction to our program
    def bmi_intro():
        print("Welcome to my BMI calculator!")
        print("If you can tell me your weight and height")
        print("I can tell you your Body Mass Index")
        print("Let's Go!\n")

#From this point I will ask the user for his/her information.
    def get_users_info():
        get_height = float(input("Please enter your height in inches. "))
        get_weight = float(input("Please enter your weight in pounds. "))
#user will enter there information above and we will then calcualte.
    body_mass_index = (get_weight * 703) / (get_height ** 2)

if body_mass_index < 18.5:
    print("A person with a BMI of " + str(body_mass_index ) + " is underwieght ")
elif body_mass_index < 24.9:
    print("A person with a BMI of " + str(body_mass_index ) + " is normal weight ")
else:
    print("A person with a BMI of " + str(body_mass_index ) + " is overweight ")

#5

So essentially my code is not in the right order?
I’m not sure how to assign values to get_weight and get_height so the user can input their numbers without causing the ZeroDivisionError: float divsion by zero
I tried everything I could think of.


#6

Yes, the order of your program is one of the other things that’ll stop your code from working which I hinted at in my previous post.

Python is an interpreted language, so you have to do some things in a particular order unlike in some other languages like C which is more flexible on order. If we did the following:

def my_function(x):
  print("This is my function! You said: ", x)

my_function("booya!")
other_function("Get schwifty!")

def other_function(s):
  if s == "Get schwifty!":
    print("Calm down, Morty.")
  else:
    print("Aww geez Rick")

we’ll get an error from Python that the function other_functions() is undefined, because Python has got to the line where we call the function before it gets to the section where we told it about the function.

You’ll need to refactor your program before you’ll be able to call main() from your try block. (I’m assuming you’d be calling main() from the try block…) :slight_smile:


#7

I apologize but I’m still lost. I got rid of the try/exception part because technically I haven’t learned that yet. I had to google it.


#8

what your code looks like now then? Please see this topic:

How do I format code in my posts?

about format, so indent of your code shows up.

You can’t call functions you haven’t defined yet.

Functions are like tasks, i can’t tell you to go do task x if i haven’t yet defined what task x is.


#9

def main():
bmi_intro()
get_users_info()

#define variables
get_height = 0.0
get_weight = 0.0

#We will start with the introduction to our program
def bmi_intro():
print(“Welcome to my BMI calculator!”)
print(“If you can tell me your weight and height”)
print(“I can tell you your Body Mass Index”)
print(“Let’s Go!\n”)

#From this point I will ask the user for his/her information.
def get_users_info():
get_height = float(input("Please enter your height in inches. "))
get_weight = float(input("Please enter your weight in pounds. "))
#user will enter there information above and we will then calcualte.
body_mass_index = (get_weight * 703) / (get_height ** 2)

if body_mass_index < 18.5:
print("A person with a BMI of " + str(body_mass_index ) + " is underwieght ")
elif body_mass_index < 24.9:
print("A person with a BMI of " + str(body_mass_index ) + " is normal weight ")
else:
print("A person with a BMI of " + str(body_mass_index ) + " is overweight ")

main()


#10

why is your main function not at the end of your program?


#11

I edited it to show main()


#12

No need to apologise, there’s nothing wrong with playing around with a new programming structure to learn how it works. :+1:

Unfortunately I’m pretty sure that your code there will still throw an error.

Your program is still attempting to calculate the BMI using zero-value variables get_height and get_weight because that line is before the call to main which, in turn, calls get_users_info. So, you’ll end up with another ZeroDivisionError.


#13

how do i give get_height and get_weight value without using 0.0
I don’t know what the user will input for their numbers to figure out their BMI.


#14

def main():
bmi_intro()

#define variables
get_height = 0.0
get_weight = 0.0
body_mass_index = 0.0

#We will start with the introduction to our program
def bmi_intro():
print(“Welcome to my BMI calculator!”)
print(“If you can tell me your weight and height”)
print(“I can tell you your Body Mass Index”)
print(“Let’s Go!\n”)

#From this point I will ask the user for his/her information.

get_height = float(input("Please enter your height in inches. "))
get_weight = float(input("Please enter your weight in pounds. "))
#user will enter there information above and we will then calcualte.
body_mass_index = (get_weight * 703) / (get_height ** 2)

if body_mass_index < 18.5:
print("A person with a BMI of " + str(body_mass_index ) + " is underwieght ")
elif body_mass_index < 24.9:
print("A person with a BMI of " + str(body_mass_index ) + " is normal weight ")
else:
print("A person with a BMI of " + str(body_mass_index ) + " is overweight ")

main()

#I rewrote my code and its not showing my introduction first


#15

The problem is not that you’re giving those variables the value of 0, it’s that your code tries to do the calculation with it before you’ve given the user sitting at the console chance to give them a different value.

:slight_smile:


#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.