Python Classes: Medical Insurance Project Step 11

Estimated insurance cost is not updating when I add another child. I’ve tried calling estimated_insurance_cost() inside and outside the method with the same result:

def update_age(self, new_age): self.age = new_age print("{} is now {} years old.".format(self.name, new_age)) self.estimated_insurance_cost() def update_num_children(self, new_num_children): self.new_num_children = new_num_children if new_num_children > 1: print("{} has {} children.".format(self.name, self.new_num_children)) else: print("{} has {} child.".format(self.name, self.new_num_children)) self.estimated_insurance_cost() patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0) print(patient1.name) print(patient1.age) print(patient1.sex) print(patient1.bmi) print(patient1.num_of_children) print(patient1.smoker) patient1.update_age(26) patient1.update_num_children(1)

Link to project here

It appears there is code missing. Could you please post it?

1 Like

Sorry about that! Here is the codebyte:

class Patient: def __init__(self, name, age, sex, bmi, num_of_children, smoker): self.name = name self.age = age self.sex = sex self.bmi = bmi self.num_of_children = num_of_children self.smoker = smoker def estimated_insurance_cost(self): estimated_cost = 250 * self.age - 128 * self.sex + 370 * self.bmi + 425 * self.num_of_children + 24000 * self.smoker - 12500 print("{}’s estimated insurance costs is {} dollars.".format(self.name, estimated_cost)) def update_age(self, new_age): self.age = new_age print("{} is now {} years old.".format(self.name, new_age)) self.estimated_insurance_cost() def update_num_children(self, new_num_children): self.new_num_children = new_num_children if new_num_children > 1: print("{} has {} children.".format(self.name, self.new_num_children)) else: print("{} has {} child.".format(self.name, self.new_num_children)) self.estimated_insurance_cost() def patient_profile(self, patient_information): patient_information = {} patient_information["Name"] = self.name patient_information["Age"] = self.age patient_information["Sex"] = self.sex patient_information["BMI"] = self.bmi patient_information["Number of Children"] = self.num_of_children patient_information["Smoker"] = self.smoker return patient_information patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0) print(patient1.name) print(patient1.age) print(patient1.sex) print(patient1.bmi) print(patient1.num_of_children) print(patient1.smoker) patient1.update_age(26) patient1.update_num_children(1) print(patient1.patient_profile())

You have two different but similarly named attributes num_of_children and new_num_children. What does your estimated cost function make use of?

Estimated cost function makes use of new_num_children.

You bring up a good point, I should change the attribute name to be more unique.

Does it? Double check what attributes are changing when you run your .update_num_children method and what attributes are actually used in the .estimated_insurance_cost method. Can one affect the other?

May be worth viewing the update_age method too and its relation to the estimated insurance cost.

Got it! Thanks for your help. I was using different parameters in the method than what was used in the .estimated_insurance_cost method

Changed the parameters and now it works:

class Patient: def __init__(self, name, age, sex, bmi, num_of_children, smoker): self.name = name self.age = age self.sex = sex self.bmi = bmi self.num_of_children = num_of_children self.smoker = smoker def estimated_insurance_cost(self): estimated_cost = 250 * self.age - 128 * self.sex + 370 * self.bmi + 425 * self.num_of_children + 24000 * self.smoker - 12500 print("{}’s estimated insurance costs is {} dollars.".format(self.name, estimated_cost)) def update_age(self, age): self.age = age print("{} is now {} years old.".format(self.name, age)) def update_num_children(self, num_of_children): self.num_of_children = num_of_children if num_of_children > 1: print("{} has {} children.".format(self.name, self.num_of_hildren)) else: print("{} has {} child.".format(self.name, self.num_of_children)) def update_bmi(self, bmi): self.bmi = bmi print("You have updated your BMI to {}".format(self.bmi)) def patient_profile(self, patient_information): patient_information = {} patient_information["Name"] = self.name patient_information["Age"] = self.age patient_information["Sex"] = self.sex patient_information["BMI"] = self.bmi patient_information["Number of Children"] = self.num_of_children patient_information["Smoker"] = self.smoker return patient_information patient1 = Patient("John Doe", 25, 1, 22.2, 0, 0) print(patient1.name) print(patient1.age) print(patient1.sex) print(patient1.bmi) print(patient1.num_of_children) print(patient1.smoker) patient1.update_age(26) patient1.update_num_children(1) patient1.update_bmi(26.2) print(patient1.patient_profile(patient1)) patient1.estimated_insurance_cost()
1 Like