I cant figure out what arguements to use or really even what arguements are.

#Template for Program 2
#The keyword "pass" is a placeholder that does nothing
#Move each line of original code into the appropriate module
#Add arguments to calcBMI() and displayResults() as needed

#original code -- not in modules yet
#cut and paste into appropriate modules below


#revised code broken into modules
#delete "pass" once you add code to a module
#don't forget to indent
def main():
    print("Body Mass Index (BMI) Program")
    print()
    weight = float(input("Enter your weight in pounds: "))
    height = float(input("Enter your height in inches: "))
    calcBMI()

def calcBMI():
    bmi = (weight * 703)/(height * height)
    displayResults()


def displayResults(bmi):
    print()
    print("Your BMI is:", bmi)

#Call the main function to get things started

main()

Dani AI

Generated

For the immediate issue is scope: weight and height in main() are local to that function, so calcBMI() can’t see them unless they are passed in. ’s approach — have main handle input, pass values into a calculator function, and let that function return the numeric result — is the right pattern. Keep functions focused: one for input, one for computation, one for presentation.

A couple of practical improvements not shown above: validate input to reject non-numeric or non-positive values, guard against division-by-zero, format the BMI for readability, and optionally return a category (underweight/normal/overweight/obese) so the display function can show both number and meaning. That makes the program safer and easier to test.

Example of a compact, more robust approach (input validation, a pure calculator, and a small categorizer):

def get_positive(prompt):
    while True:
        try:
            v = float(input(prompt))
        except ValueError:
            print("Please enter a number.")
            continue
        if v <= 0:
            print("Enter a value greater than 0.")
            continue
        return v

def calc_bmi_lbs(weight_lbs, height_in):
    if height_in <= 0:
        raise ValueError("height must be > 0")
    return (weight_lbs * 703.0) / (height_in ** 2)

def bmi_category(bmi):
    if bmi < 18.5:
        return "Underweight"
    if bmi < 25:
        return "Normal"
    if bmi < 30:
        return "Overweight"
    return "Obese"

Quick troubleshooting tips: a NameError mentioning weight in calcBMI means the value wasn't passed in. If the result looks wrong, test the calculator with a known case (use a small assert) and print intermediate values to check units. Avoid globals — pass values explicitly and return results for cleaner, testable code.

Recommended Answers

All 2 Replies

Greetings:

First and foremost, I warmly applaud your efforts to learn Python.

That being said, arguments in Python are parameters(or local variables) that are passed(or given) to a function to work with.

Your calcBMI function is currently not set-up to receive any parameters. Also, it's a better programming practice to let your main function execute all of the functions in your program. Here's a fix:

def main():
    print("Body Mass Index (BMI) Program")
    print()
    weight = float(input("Enter your weight in pounds: "))
    height = float(input("Enter your height in inches: "))
    bmi = calcBMI(weight, height)
    displayResults(bmi)

def calcBMI(weight, height):
    return (weight * 703) / (height * height)

def displayResults(bmi):
    print()
    print("Your BMI is:", bmi)

if __name__ == "__main__":
    main()
commented: Nice answer :) +15

Thank you!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.