I am working on the Insurance Portfolio Project and I believe I have been doing pretty well but there is something wrong and after a couple hours I can’t seem to figure out what.
I set a variable patient_info
equal to the class Patients
and plugged in the lists from the data as the parameters for Patients
(located at the bottom of the code) but am getting this error.
Could have other errors too that aren’t throwing error messages so if you see something please bring it to my attention as well! Thank you in advance!
ps: (not sure if I’m using the line break function (’\n’) correctly)
import csv
import statistics
ages = []
sexes = []
bmis = []
num_children = []
smoker_status = []
regions = []
insurance_charges = []
def load_lists(lst, file, column):
with open(file) as insurance_csv:
insurance_reader = csv.DictReader(insurance_csv)
for row in insurance_reader:
lst.append(row[column])
return lst
ages = load_lists(ages, 'insurance.csv', 'age')
sexes = load_lists(sexes, 'insurance.csv', 'sex')
bmis = load_lists(bmis, 'insurance.csv', 'bmi')
num_children = load_lists(num_children, 'insurance.csv', 'children')
smoker_status = load_lists(smoker_status, 'insurance.csv', 'smoker')
regions = load_lists(regions, 'insurance.csv', 'region')
insurance_charges = load_lists(insurance_charges, 'insurance.csv', 'charges')
class Patients:
def __init__(self, patient_age, patient_sex, patient_bmi, patient_num_children, patient_smoker_status, patient_regions, patient_insurance_charges):
self.patient_age = patient_age
self.patient_sex = patient_sex
self.patient_bmi = patient_bmi
self.patient_num_children = patient_num_children
self.patient_smoker_status = patient_smoker_status
self.patient_regions = patient_regions
self.patient_insurance_charges = patient_insurance_charges
def average_age(self, patient_age):
sum_age = 0
for age in patient_age:
sum_age += int(age)
avg_age = (sum_age)/(len(patient_age))
return ('The average age of all patients is: ', str(round(avg_age, 2)))
def analyze_sexes(self, patient_sex):
male = 0
female = 0
for sex in patient_sex:
if sex == 'male':
male += 1
else:
female += 1
return
def region_analysis(self, patient_regions):
south_west = 0
south_east = 0
north_west = 0
north_east = 0
for place in patient_regions:
if place == 'southwest':
south_west +=1
if place == 'southeast':
south_east +=1
if place == 'northwest':
north_west +=1
if place == 'north_east':
north_east += 1
print('There are ' + str(south_west) + ' patients in the southwest')
print('There are ' + str(south_east) + ' patients in the southeast')
print('There are ' + str(north_west) + ' patients in the northwest')
print('There are ' + str(north_east) + ' patients in the northeast')
def average_cost_analysis(self, patient_insurance_charges):
sum_cost = sum(patient_insurance_charges)
avg_cost = sum_cost / len(patient_insurance_charges)
return ('The average insurance cost is: ' + str(avg_cost))
def smoker_v_nonsmoker(self, patient_smoker_status):
smokers = 0
nonsmokers = 0
for smoke in patient_smoker_status:
if smoke == 'yes':
smokers += 1
else:
nonsmokers += 1
return (f'The number of smokers is: ' + str(smokers),'\n' 'The number of non-smokers is: ' + str(nonsmokers))
def kids_v_nokids(self, patient_num_children):
no_kids = 0
kids = 0
for kid in patient_num_children:
if int(kid) == 0:
kids += 1
else:
no_kids += 1
return (f'There are ' + str(no_kids) + ' patients with no kids.\n', 'There are ' + str(kids) + ' patients with kids.')
def dictionary(self):
self.patient_dict = {}
self.patient_dict['age'] = [int(ages) for ages in self.patient_age]
self.patient_dict['sex'] = self.patient_sex
self.patient_dict['bmi'] = self.patient_bmi
self.patient_dict['children'] = self.patient_num_children
self.patient_dict['smoker'] = self.patient_smoker_status
self.patient_dict['region'] = self.patient_regions
self.patient_dict['charges'] = self.patient_insurance_charges
return self.patient_dict
patient_info = Patients(ages, sexes, bmis, num_children, smoker_status, regions, insurance_charges)
patient_info.average_age(ages)
# patient_info.average_cost_analysis()