Home > Software design >  What can I do to make my BMI calculator operable?
What can I do to make my BMI calculator operable?

Time:02-26

I am trying to figure out how to use modules for a BMI program, but I keep getting issues with the variables and them either not being defined, or overwritten in a manner that would have them work themselves out properly.

Main code:

from getBMI import getBMI

# The Main Function
def main ():
    weight = input("What would you say your current weight is? ")
    weight = float(weight)
    BMI = 0
    getBMI()
    print("Your BMI is "   str(BMI))

# Calling the getBMI module
def getBMI():
    weight = 0
    BMI = weight * 703 / (weight * weight)

# Calling Main Function
main()

getBMI's code:

def main ():
    weight = input("What would you say your current weight is? ")

def getBMI():
    weight = 0
    BMI = weight * 703 / (weight * weight)

main()

I want to be able to let this work along with having the weight variable defined, or else the code will simply not work.

CodePudding user response:

You are close, but there are some key points to know about using functions within python

# The Main Function
def main ():
    weight = input("What would you say your current weight is? ")
    weight = float(weight)
    BMI = getBMI(weight)
    print("Your BMI is "   str(BMI))

# Calling the getBMI module
def getBMI(weight):
    BMI = weight * 703 / (weight * weight)
    return BMI

# Calling Main Function
main()

I'm not sure of your logic, but based on your code I believe you should be able to alter your math if its not what you want.

Essentially we define the 2 functions then call the main function at the end.

The main function takes an input of weight and alters it to a float variable, then you set the BMI variable to a call of the getBMI function with a weight parameter (which is just your weight variable from earlier passed into it).

The getBMI function uses the weight variable that you passed to it from the main function and does the math for the BMI variable in the getBMI function. Once the math is completed you use the return option to send the results of BMI back to the BMI variable in the main function. after that you simply are printing the results of your BMI variable.

  • Related