nojaa
October 28, 2020, 12:31am
1
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
nojaa
October 28, 2020, 1:19am
3
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
For the final code in the link below I can’t get code to work and not sure why. It just says “SyntaxError: unexpected E0F while parsing”. Can someone please help. The code i’m talking about is the following:
for name, record in medical_records.items():
script.py
# Add your code here
medical_costs = {}
medical_costs["Marina"] = 6607.0
medical_costs["Vinay"] = 3225.0
medical_costs.update({"Connie": 8886.0, "Isaac": 16444.0, "Valentina": 6420})
medical_costs["Vinay"] = 3325.0
total_cost = 0
for cost in medical_costs.values():
This file has been truncated. show original
Thank you!
Check your parentheses in the final line of code.
1 Like
Thank you. Found the issue. Working now.
I keep getting NameError when trying to run this function, any idea why?
Hi there.
It would be helpful if you also posted your code; you can find instructions on how to do that by clicking here .
NameError
would suggest that you are attempting to use a variable which has not been declared yet.
Hi all,
concerning task #14 , I was wondering if there’s a shorter way to add the medical records INSTEAD of writing all the following:
medical_records["Vinay"] = {"Age": 24, "Sex": "Male", "BMI": 26.9, "Children": 0, "Smoker": "Non-smoker", "Insurance_cost": 3225.0}
medical_records["Connie"] = {"Age": 43, "Sex": "Female", "BMI": 25.3, "Children": 3, "Smoker": "Non-smoker", "Insurance_cost": 8886.0}
medical_records["Isaac"] = {"Age": 35, "Sex": "Male", "BMI": 20.6, "Children": 4, "Smoker": "Smoker", "Insurance_cost": 16444.0}
medical_records["Valentina"] = {"Age": 52, "Sex": "Female", "BMI": 18.7, "Children": 1, "Smoker": "Non-smoker", "Insurance_cost": 6420.0}
Thanks in advance for your help
Hello,
I keep getting a syntax error for the 1st line, “def update_medical_records” utilizing your code.
Did you have this issue?
Thanks.
That would appear to be the precise issue that they were posting about, from reading the OP…
Hi,
I have tried to write this code for hours
For the ones who are looking for answers, finally I got another way to do this
Leaving it here.
dictionary update function
def update_medical_records(name, age, sex, bmi, children, smoker, insurance_cost, marriage):
medical_records[name] = {"Age": age, "Sex": sex, "BMI": bmi, "Children": children, "Smoker": smoker, "Insurance_cost": insurance_cost, marriage: "Married"}
update_medical_records('Vinay', 25, 'Female', 23, 2, '', 555, 'Married')
print(medical_records)
def update_medical_recordss(dictionary):
medical_records.update(dictionary)
This file has been truncated. show original
Wish you all good luck!