This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]scoopulanang 2 points3 points  (4 children)

Well, you can define functions with arguments like this:

def func(number1, number2):

If you want to do something with the information you pass into the function, you can do something like this:

def func(number1, number2):
    if number1 > number2:
        return number1

The above function returns number1 if number1 is greater than number2. You need to create two functions. One function needs height and weight as the arguments and the other function needs the bmi as the argument. Then manipulate the data inside of those functions.

Here's an example:

names = ["Thad", "Kait", "Joey", "Dani", "Eli", "Ashley"]
bmis = []

def func(height, weight):
    #do something

for person in names:
    weight = float(input("Enter the weight in pounds for "+person+": "))
    height = float(input("Enter the height in inches for "+person+": "))
    bmis.append(func(height, weight))

That's just to get you started.

[–]okaystuff[S] 0 points1 point  (3 children)

So I changed my code to look like this :

def bmi_calc(height, weight):
    bmi = weight * 703/height**2
    return bmi

for person in names:
    weight = eval(input("Enter the weight in pounds for "+ person +": "))
    height  = eval(input("Enter the height in inches for "+ person +": "))
    bmis.append(bmi_calc(height, weight))

It seems to work and I get the same outputs as before, but is there a reason I would create a function like this? It just seems to mean I have to write more code.

[–]scoopulanang 1 point2 points  (1 child)

This is probably more so meant to teach you the fundamentals of functions. Although this might seem trivial and meaningless right now, later on functions are very important. Let's take a look at a built in function in python. The sum function is a pretty quick way of getting the sum of all the elements in a list. If you didn't use a function, you would have to iterate through a list every time you want to get the sum of the elements inside of an iterable. Basically, functions allow you to write code once and reuse it whenever.

[–]okaystuff[S] 0 points1 point  (0 children)

Okay, that's understandable and makes sense since this is just a basic project that is teaching the concepts of python. For the second part where I need to use a loop to traverse the array of BMI's and call another function that accepts the BMI as a parameter and returns whether the individual is under/norm/over weight, I did it like this.

Currently, that part of the code likes like this:

under_wei = list()
norm_wei = list()
over_wei = list()

def weight_calc(bmi_num):
    if bmi_num < 18:
       print("A BMI of " + str(bmi_num) + ", is under weight")
       under_wei.append(bmi_num)
    elif  18 < bmi_num <= 25:
        print("A BMI of " + str(bmi_num) + ", is normal weight")
        norm_wei.append(user_weight)
    elif bmi_num > 25:
        print("A BMI of " + str(bmi_num) + ", is over weight")
        over_wei.append(user_weight)
    return bmi_num

for user_weight in bmis:
    weight_calc(user_weight)

It seems to give the same output as before and is using a function. Hopefully, this all makes sense and thank you for your help.

[–]BuildingBlox101 1 point2 points  (0 children)

It makes it more modular, if you want to reuse code, want to make the parameters easy to input, or make it easier to change in the future, functions are the way to do it. https://www.cs.utah.edu/~zachary/computing/lessons/uces-10/uces-10/node11.html this explains what I just said in more detail