Answered! Need help creating a code in python (python only please). Given a data file, find the max, min, and average values of columns….

Need help creating a code in python (python only please).

Given a data file, find the max, min, and average values of columns. Also, create an addition column that is based on the other columns.

Data is arranged in columns where each item of data is twelve columns wide (so it is easy to extract data items using slicing):

Name Height(m) Weight(kg)

Joe 1.82 72.57

Mary 1.60 63.50

Dion 1.90 90.71

Kayla 1.72 66.31

Jose 1.78 70.23

Sofia 1.63 65.12

Erik 1.98 92.21

Sara 1.57 65.77

I need help creating a code to calculate the max, min, and average of the height and weight columns and to add an additional column that has the BMI calculated based on each height and weight.  format string: “{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”

once this is completed, I need help writing that output above to a file.

Example Output should look like:

Name Height(m) Weight(kg) BMI

Joe 1.82 72.57 21.91

Mary 1.60 63.50 24.80

Dion 1.90 90.71 25.13

Kayla 1.72 66.31 22.41

Jose 1.78 70.23 22.17

Sofia 1.63 65.12 24.51

Erik 1.98 92.21 23.52

Sara 1.57 65.77 26.68

Average 1.75 73.30 23.89

Max 1.98 92.21 26.68

Min 1.57 63.50 21.91

I have written two programs for your assignment. One which writes the output to standard output and other to a file.

PART A:
Code:

#!/usr/local/bin/python3

# program to calculate BMI and display on standard output

# script name: lab06a.py

def main():
# opening the file data.txt
with open(“data.txt”) as fp:
# initializing the variables
(max_weight, min_weight, sum_weight, avg_weight) = (0, 10**9, 0, 0)
(max_height, min_height, sum_height, avg_height) = (0, 10**9, 0, 0)
(max_bmi, min_bmi, sum_bmi, avg_bmi) = (0, 10**9, 0, 0)
n = 0
for line in fp:
if ‘Name’ in line:
print (“{:<12s} {:<12s} {:<12s} {:<12s}”.format(‘Name’, ‘Height(m)’, ‘Weight(kg)’, ‘BMI’))
else:
# splitting the line from file and storing into variables
(name, height, weight) = line.split()

# converting weight and height as float variables
weight = float(weight)
height = float(height)

# identifying max and min from the weight numbers
if weight > max_weight:
max_weight = weight
if weight < min_weight:
min_weight = weight
sum_weight = sum_weight + weight

# identifying max and min from the height numbers
if height > max_height:
max_height = height
if height < min_height:
min_height = height
sum_height = sum_height + height

# calculating bmi and identifying min and max from the bmi numbers
bmi = weight / height ** 2
if bmi > max_bmi:
max_bmi = bmi
if bmi < min_bmi:
min_bmi = bmi
sum_bmi = sum_bmi + bmi

n = n + 1
print (“{:<12s} {:<12.2f} {:<12.2f} {:<12.2f}”.format(name, height, weight, bmi))

# calculating average weight, height and bmi
avg_weight = sum_weight / n
avg_height = sum_height / n
avg_bmi = sum_bmi / n
print (“n”)

# printing average, max, min statistics
print (“{:<12s} {:<12.2f} {:<12.2f} {:<12.2f}”.format(‘Average’, avg_height, avg_weight, avg_bmi))
print (“{:<12s} {:<12.2f} {:<12.2f} {:<12.2f}”.format(‘Max’, max_height, max_weight, max_bmi))
print (“{:<12s} {:<12.2f} {:<12.2f} {:<12.2f}”.format(‘Min’, min_height, min_weight, min_bmi))

fp.close()

if __name__==’__main__’:
main()

Execution and output:
Unix Terminal> cat data.txt
Name Height(m) Weight(kg)
Joe 1.82 72.57
Mary 1.60 63.50
Dion 1.90 90.71
Kayla 1.72 66.31
Jose 1.78 70.23
Sofia 1.63 65.12
Eric 1.98 92.21
Sara 1.57 65.77
Unix Terminal> python3 lab06a.py
Name Height(m) Weight(kg) BMI
Joe 1.82 72.57 21.91
Mary 1.60 63.50 24.80
Dion 1.90 90.71 25.13
Kayla 1.72 66.31 22.41
Jose 1.78 70.23 22.17
Sofia 1.63 65.12 24.51
Eric 1.98 92.21 23.52
Sara 1.57 65.77 26.68

Average 1.75 73.30 23.89
Max 1.98 92.21 26.68
Min 1.57 63.50 21.91

PART B:
Code:

#!/usr/local/bin/python3

# program to calculate BMI and write to a file
# script name: lab06b.py

def main():
# opening the file out.txt for writing the output
outfile = open(“output.txt”, “w”)

# opening the file data.txt for reading
with open(“data.txt”) as fp:
# initializing the variables
(max_weight, min_weight, sum_weight, avg_weight) = (0, 10**9, 0, 0)
(max_height, min_height, sum_height, avg_height) = (0, 10**9, 0, 0)
(max_bmi, min_bmi, sum_bmi, avg_bmi) = (0, 10**9, 0, 0)
n = 0
for line in fp:
if ‘Name’ in line:
print (“{:<12s} {:<12s} {:<12s} {:<12s}”.format(‘Name’, ‘Height(m)’, ‘Weight(kg)’, ‘BMI’), file=outfile)
else:
# splitting the line from file and storing into variables
(name, height, weight) = line.split()

# converting weight and height as float variables
weight = float(weight)
height = float(height)

# identify max and min from the weight numbers
if weight > max_weight:
max_weight = weight
if weight < min_weight:
min_weight = weight
sum_weight = sum_weight + weight

# identifying max and min from the height numbers
if height > max_height:
max_height = height
if height < min_height:
min_height = height
sum_height = sum_height + height

# calculating bmi and identifying min and max from the bmi numbers
bmi = weight / height ** 2
if bmi > max_bmi:
max_bmi = bmi
if bmi < min_bmi:
min_bmi = bmi
sum_bmi = sum_bmi + bmi

n = n + 1
print (“{:<12s} {:<12.2f} {:<12.2f} {:<12.2f}”.format(name, height, weight, bmi), file=outfile)

# calculating average weight, height and bmi
avg_weight = sum_weight / n
avg_height = sum_height / n
avg_bmi = sum_bmi / n
print (“n”, file=outfile)

# printing average, max, min statistics
print (“{:<12s} {:<12.2f} {:<12.2f} {:<12.2f}”.format(‘Average’, avg_height, avg_weight, avg_bmi), file=outfile)
print (“{:<12s} {:<12.2f} {:<12.2f} {:<12.2f}”.format(‘Max’, max_height, max_weight, max_bmi), file=outfile)
print (“{:<12s} {:<12.2f} {:<12.2f} {:<12.2f}”.format(‘Min’, min_height, min_weight, min_bmi), file=outfile)

fp.close()
outfile.close()

if __name__==’__main__’:
main()

Execution and output:
Unix Terminal> python3 lab06b.py
Unix Terminal> cat output.txt
Name Height(m) Weight(kg) BMI
Joe 1.82 72.57 21.91
Mary 1.60 63.50 24.80
Dion 1.90 90.71 25.13
Kayla 1.72 66.31 22.41
Jose 1.78 70.23 22.17
Sofia 1.63 65.12 24.51
Eric 1.98 92.21 23.52
Sara 1.57 65.77 26.68

Average 1.75 73.30 23.89
Max 1.98 92.21 26.68
Min 1.57 63.50 21.91
Unix Terminal>