Question: Python programming classes- Define a class called Patient. Feed one line from the CSV into Patient at a time, into the class constructor, which should
Python programming classes-
Define a class called Patient. Feed one line from the CSV into Patient at a time, into the class constructor, which should parse the line and set the variables gender, height and weight. You should construct a list of instances of class Patient from all the data in the CSV file. Additionally, the class should have a method .bmi() to calculate and store a column associated with bmi, another method .get_health_status() which classifies the health status (using bmi) as health, overweight or underweight. Finally have a method print_to_file() which prints the data from the class out to a file, one index of the list of your class at a time.
import csv import matplotlib.pyplot as plt
class bmi: def __init__(self): self.filename='bmi.csv'; #opening file def readFile(self): obj=open(self.filename) obj_reader=csv.reader(obj,delimiter=' ') male=[] # having male's bmi female=[] # having female bmi file_obj2=open("bmi_health.csv","w") # opening file for adding bmi and status for val in obj_reader: bmi=(int(val[1])*2.2*703)/(int(val[2])*int (val[2])) # bmi calculation bmi=round( bmi,2) file_obj2.write(val[0]) file_obj2.write(' ') file_obj2.write(val[1]) file_obj2.write(' ') file_obj2.write(val[2]) file_obj2.write(' ') file_obj2.write("%0.2f "%bmi) file_obj2.write(' ') if val[0]=='male': # checking for male male.append(bmi) if bmi>=19 and bmi<=25: # bmi healthy status file_obj2.write('healthy') elif bmi>25: file_obj2.write('obese') else: file_obj2.write('underweight') else: # checking for male female.append(bmi) if bmi>=19 and bmi<=25: # bmi healthy status file_obj2.write('healthy') elif bmi>25: file_obj2.write('obese') else: file_obj2.write('underweight') file_obj2.write(' ') print(" BMI list for male: ") print(male) # male bmi calculation list print(" BMI list for female: ") print(female) #female bmi calculation list obj.close() # closing old file file_obj2.close() #closing new file plt.ylabel('NUMBER') plt.xlabel('BMI') plt.title('HISTROGRAM OF BMI OF MALE & FEMALE ') plt.hist([male,female]) plt.show() ob=bmi() ob.readFile()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
