Answered! Part B: The second task is to write that output to a file. The steps are 1. Open a file for writing, e.g. outfile = open(“output.txt”,”w”)…

a Amazon.com: Kuzy BLACI x ht Macbook Case I Glaze P x f Microsoft Word labo6.doc x C Chegg Study Guided Sol x Mf Lab Exercises C O www.cse.msu.ed cse231/Online/Labs/Labo6/labo6.pdf Apps For quick access, place your bookma s here on bookmarks bar. mport bookmarks now. Part A: Data Processing 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. There will be no error checking required for this lab, You are provided with a data file named (surprise!) data.txt. Data is arranged in columns where each item of data is twelve columns wide (so it is casy to extract data items using slicing): Name Height (m) Weight (kg) Joe 1.82 72.57 Mary 1.60 63.50 Dion 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 Your task is read the file data.txt and 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. Your output will look like this (and needs to be formatted like this. To match the testing on Mirmir here is the format string <12s <12.2f <12.2f <12.2f Height (m) Weight (kg) BMI Name Joe 1.82 72.57 21 91 1.60 63.50 24.BD Mary Dion 1.90 90.71 25.13 Kayla 1.72 66.31 22.41 78 70.23 22.17 Jose Sofia. 1.63 65.12 24.51 1.98 92.21 23.52 Erik Sara 1.57 65.77 26.68 Average 1.75 73.30 23.89 Max 1.98 92.21 26.68 1.57 63.50 21 91 Min The formula for BMI is simple in the metric system: BMI weight/height *2 How do we find the max and min? x 5 Ways to Take a screensho x Bobby

Part B:

The second task is to write that output to a file.

The steps are 1. Open a file for writing, e.g. outfile = open(“output.txt”,”w”)

2. Whenever you print, include the argument file = outfile For example, if you previously had print(x) you will now have print(x, file = outfile)

3. Close the file, e.g. outfile.close()

Show transcribed image text

Expert Answer

 PYTHON CODE FOR PART A:

import sys

f=open(‘data.txt’)

maxi_height=-(sys.maxsize)
mini_height=sys.maxsize
maxi_weight=-(sys.maxsize)
mini_weight=sys.maxsize
total_height=0
total_weight=0
count_height=0
count_weight=0

print(“{:12s}{:12s}{:12s}{:12s}”.format(“Name”,”Height(m)”,”Weight(kg)”,”BMI”))

for n,line in enumerate(f):

if n==0:
continue

data=line.split()

if data:
height=float(data[1])
weight=float(data[2])
total_height+=height
total_weight+=weight
count_height+=1
count_weight+=1
bmi=weight/height ** 2
print(“{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”.format(data[0],height,weight,bmi))

if height > maxi_height:
maxi_height=height

if height < mini_height:
mini_height=height

if weight > maxi_weight:
maxi_weight=weight

if weight < mini_weight:
mini_weight=weight

avg_height= total_height/count_height
avg_weight=total_weight/count_weight
avg_bmi=avg_weight/(avg_height ** 2)

bmi2=maxi_weight/(maxi_height ** 2)
bmi3=mini_weight/(mini_height ** 2)
print(“n{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”.format(“Average”,avg_height,avg_weight,avg_bmi))
print(“{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”.format(“Average”,maxi_height,maxi_weight,bmi2))
print(“{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”.format(“Average”,mini_height,mini_weight,bmi3))

SCREENSHOT FOR OUTPUT

PYTHON CODE FOR PART B:

import sys

f=open(‘data.txt’)
output=open(‘output.txt’,’w’)

maxi_height=-(sys.maxsize)
mini_height=sys.maxsize
maxi_weight=-(sys.maxsize)
mini_weight=sys.maxsize
total_height=0
total_weight=0
count_height=0
count_weight=0

print(“{:12s}{:12s}{:12s}{:12s}”.format(“Name”,”Height(m)”,”Weight(kg)”,”BMI”),file=output)

for n,line in enumerate(f):

if n==0:
continue

data=line.split()

if data:
height=float(data[1])
weight=float(data[2])
total_height+=height
total_weight+=weight
count_height+=1
count_weight+=1
bmi=weight/height ** 2
print(“{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”.format(data[0],height,weight,bmi),file=output)

if height > maxi_height:
maxi_height=height

if height < mini_height:
mini_height=height

if weight > maxi_weight:
maxi_weight=weight

if weight < mini_weight:
mini_weight=weight

avg_height= total_height/count_height
avg_weight=total_weight/count_weight
avg_bmi=avg_weight/(avg_height ** 2)

bmi2=maxi_weight/(maxi_height ** 2)
bmi3=mini_weight/(mini_height ** 2)
print(“n{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”.format(“Average”,avg_height,avg_weight,avg_bmi),file=output)
print(“{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”.format(“Average”,maxi_height,maxi_weight,bmi2),file=output)
print(“{:<12s}{:<12.2f}{:<12.2f}{:<12.2f}”.format(“Average”,mini_height,mini_weight,bmi3),file=output)

Still stressed from student homework?
Get quality assistance from academic writers!