Python dictionaries (data science - medical insurance project)

Hi,
I am trying to figure out how to update a dictionary. Specifically, I am trying to figure out #19 in the medical insurance project (https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-dictionaries/projects/ds-python-dictionaries-project)

#19 wants you to write a function that will take the name and the person’s medical record into the medical records dictionary.

The medical records need to have the following form:

{'Marina': {'Age': 27, 'Sex': 'Female', 'BMI': 31.1, 'Children': 2, 'Smoker': 'Non-smoker', 'Insurance_cost': 6607.0}, 'Vinay': {'Age': 24, 'Sex': 'Male', 'BMI': 26.9, 'Children': 0, 'Smoker': 'Non-smoker', 'Insurance_cost': 3225.0}, ...}

I have tried writing the following but only received a syntax error but nothing else.

def update_medical_records(name, age, sex, bmi, children smoker, insurance_cost):
  medical_records_dict[name] = {"Age": age, "Sex": sex, "BMI": bmi, "Children": children, "Smoker": smoker, "Insurance_cost": insurance_cost}
  medical_records.update(medical_records_dict)

Any ideas why this function won’t work?

Thank you so much for helping!

@nojaa, welcome to the forums!

It looks like you’re missing a comma between children and smoker in your function. That is likely what is throwing the syntax error.

2 Likes

Oh my goodness, you are right! Thank you so much!!!

Furthermore, I noticed that I needed to add an empty dictionary first for it to work.

def update_medical_records(name, age, sex, bmi, children, smoker, insurance_cost):
  medical_records_dict = {}
  medical_records_dict[name] = {"Age": age, "Sex": sex, "BMI": bmi, "Children": children, "Smoker": smoker, "Insurance_cost": insurance_cost}
  medical_records.update(medical_records_dict)
2 Likes