Python Dictionaries: Medical Insurance Project/ help with #19

https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-dictionaries/projects/ds-python-dictionaries-project

Hi, can someone help, please? I need help understanding why I’m getting a syntax error in my code.
So, I’m supposed to write a function that takes names and details of patients and updates it to the “medical_records”. It should look something like this:

{‘Marina’: {‘Age’: 27, ‘Sex’: ‘Female’, ‘BMI’: 31.1, ‘Children’: 2, ‘Smoker’: ‘Non-smoker’, ‘Insurance_cost’: 6607.0}

My code is this:

def update_medical_records(names, ages, gender, bmis, children, Smoker, insurance_cost):
  return medical_records.update([name] = {"Age": ages, "Sex": gender, "BMI": bmis, Children: children, "Smoker": Smoker, "Insurance_cost": insurance_cost})

But I keep getting this:

File "script.py", line 45
    return  medical_records.update([name] = {"Age": ages, "Sex": gender, "BMI": bmis, Children: children, "Smoker": Smoker, "Insurance_cost": insurance_cost})
                                  ^
SyntaxError: keyword can't be an expression

I don’t really understand what they mean by that?

I looked around for an answer and I found this that was posted some time ago. The biggest difference I could spot was that in the end the person realised they needed an empty list. Would you really need and empty list to solve this problem?

Also, if I wanted to call it would this be ok?
print(update_medical_records(["Ros"] = {4, "Female", 36.0, 0, "non-smoker", 6584.0}))

You should double check what the .update method actually does for a dictionary: https://docs.python.org/3/library/stdtypes.html#dict.update

Here’s a simplified example of the expected syntax and usage-

dct = {}
dct.update({"key": "value"})

Bear in mind that like most methods designed to mutate an existing object it is expected to return None and therefore using it with a return statement will not do what you want. If my meaning is not clear try print(dct.update({"key": "value"})) to see what is actually returned.

The “expression” you have used is [name] and if you consider writing that line on its own you’ll see that it is the syntax for a list literal. As the error states you can’t use that expression here. Only when following a valid object identifier is that syntax used for indexing, e.g. mydict["name"] so using it alone will not do what you seem to expect.

If you change your code to follow how .update works then you should be able to meet this task, perhaps the plural argument identifiers (names, ages, bmis) etc. would be more suitable as singular since as written you only pass the data of a single patient but that’s just me being picky :wink:.

Thanks tgrtim, I’m a little confused so I going to start all over again. Btw, I tried printing
print(dct.update({"key": "value"})) but got nothing. Was that the point?

1 Like

Hi, I don’t understand what the question means. Could you help me what to expect from this question?

here is the question

  • Create a function called update_medical_records() that takes in the name of an individual as well as their medical data, and then updates the medical_records dictionary accordingly.

here https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-dictionaries/projects/ds-python-dictionaries-project

From completing the previous steps you should have a dictionary named medical_costs which contains details like the following:

'Vinay': {
    'Age': 24,
    'BMI': 26.9,
    'Children': 0,
    'Insurance_cost': 3225.0,
    'Sex': 'Male',
    'Smoker': 'Non-smoker'
}

The task is to create a function which can add additional individuals to this dictionary. As described it should take an individual’s name e.g. “Vinay” and their medical details, the {‘Age’: 24, ‘BMI’: 26.9 … etc. etc.} part, and update the existing dictionary (so your function should accept these values as arguments).

The result should look like this?

{‘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}, ‘Connie’: {‘Age’: 43, ‘Sex’: ‘Female’, ‘BMI’: 25.3, ‘Children’: 3, ‘Smoker’: ‘Non-smoker’, ‘Insurance_cost’: 8886.0}, ‘Issac’: {‘Age’: 35, ‘Sex’: ‘Male’, ‘BMI’: 20.6, ‘Children’: 4, ‘Smoker’: ‘Smoker’, ‘Insurance_cost’: 16444.0}, ‘Valentina’: {‘Age’: 52, ‘Sex’: ‘Female’, ‘BMI’: 18.7, ‘Children’: 1, ‘Smoker’: ‘Non-smoker’, ‘Insurance_cost’: 6420.0}}

Yes, I believe that’s how medical_records should look. Your function (optional step 19) should be capable of adding additional records to this existing dictionary.

thank you so much, appreciate your help

the code look like this :grin:

def update_medical_records(name, age, sex, bmi, child, smoker, cost):
  update_record = {}
  update_record[name] = {"Age": age, "Sex": sex, "BMI": bmi, "Children": child, "Smoker": smoker, "Insurance_cost": cost}
  medical_records.update(update_record)
  return medical_records

print(update_medical_records("Mr.Foo", 44,"Male", 21.1, 1, "Non-smoker", 6604.0))

I was struggling with 19 a bit too until I found this link on nested dictionaries:

Worth a read through even if you managed the exercise!

From that I managed to create a function that edits individual entries or adds a new key: value pair.

It looks like this:

def update_medical_records(name, field, value):
  medical_records[name][field] = value

Hope that helps!