Jessica Nielson
Jessica Nielson

Reputation: 21

Running a function within a for-loop

Trying to calculate and classify BMI from a function I created, while putting it into a FOR LOOP that will open and read the file with height and weight (bmi.txt), create new variables in addition to height and weight for BMI and classification, and write that to a new file (bmi_calculated). I've got a start on the read and write file code working (though it's flipping the columns), and the function to calculate and classify BMI, but I'm not sure how to combine them to run the function within the FOR LOOP.

BMI function:

def BMI(height, weight):
    bmi = weight / height ** 2
    bmi = round(bmi, 1)

    if bmi <= 18.5: result = 'underweight.'
    elif 18.5 < bmi <= 25: result = 'a healthy weight.'
    elif 25 < bmi <= 29.9: result = 'overweight.'
    else: result = 'obese.'

    return [height, weight, bmi, result]

h = float(input('Your height in meters: '))
w = float(input('Your weight in kilos: '))      
my_list = ["A height of", "and a weight of", "represent a BMI of", 
"which is"]
results = BMI(h, w)
print(my_list[0], results[0], my_list[1], results[1], my_list[2], 
results[2], my_list[3], results[3])

Read/write code:

infile = open("bmi.txt", "r")
outfile = open("bmi_calculated.txt", "w")
line = infile.readline()
for line in infile:
    line = line.strip()
    elements = line.split("\t")
    outfile.write(elements[1] + "\t" + elements[0] + "\n")
infile.close()
outfile.close()

EDIT: This is my attempt to call the function in the for loop, in response to one of the comments. I'm new to Python and not sure how to do this, which is why I'm asking for help:

def BMI(height, weight):
    bmi = weight / height ** 2
    bmi = round(bmi, 1)

    if bmi <= 18.5: result = 'underweight.'
    elif 18.5 < bmi <= 25: result = 'a healthy weight.'
    elif 25 < bmi <= 29.9: result = 'overweight.'
    else: result = 'obese.'

    return [height, weight, bmi, result]

infile = open("bmi.txt", "r")
outfile = open("bmi_calculated.txt", "w")
line = infile.readline()
for line in infile:
    line = line.strip()
    elements = line.split("\t")
    outfile.write(elements[1] + "\t" + elements[0] + "\n")
    print(BMI)
infile.close()
outfile.close()

This code doesn't add BMI or classification of overweight/obsese/etc to the outfile, and returns this "function BMI at 0x113fc4400" in repetition in the IDLE shell for the number for rows in the bmi.txt file.

Upvotes: 0

Views: 493

Answers (1)

Pangeran Bottor
Pangeran Bottor

Reputation: 418

I think it would be better if you get first all data from bmi.txt and then write the result(s) to another file.

my bmi.txt (two columns, tab separated)
100    100
200    200

def get_bmi_data(bmi_file):
    with open(bmi_file, "r") as f:
        raw_bmi_data = f.readlines()
    bmi_data =  [list(map(int, data.strip().split("\t"))) for data in raw_bmi_data]
    return bmi_data

bmi_data = get_bmi_data("bmi.txt")
# print(bmi_data) ->  [[100, 100], [200, 200]]

And then, for each data, you calculate and write to bmi_calculated.txt

def calculate_and_write(bmi_data):
    with open("bmi_calculated.txt", "w") as f:
        for data in bmi_data:
            height = data[0]
            weight = data[1]
            # change BMI method to return bmi and result
            bmi, result = BMI(height, weight)
            mytemplate = "A height of {} and a weight of {} represent a BMI of {} which is {}\n"
            to_write = mytemplate.format(height, weight, bmi, result)
            f.write(to_write)

Upvotes: 1

Related Questions