Python Dictionaries: Medical Insurance Project / help with #18

Hi, im working with this exercise https://www.codecademy.com/paths/data-science-foundations/tracks/dsf-python-fundamentals-for-data-science-part-ii/modules/dsf-python-dictionaries/projects/ds-python-dictionaries-project
and on task #18 we have to use a for loop to iterate through the items of medical_records, like this:

for name, record in medical_records.items():
print(name + " is a " + str(record[“Age”]) +
" year old " + record[“Sex”] + " " + record[“Smoker”] \ +
" with a BMI of " + str(record[“BMI”]) +
" and insurance cost of " + str(record[“Insurance_cost”]))

But i’m trying to use the print.format() sintax because I think it looks cleaner… but I can’t make it work…

for key, value in medical_records.items():
print("{name} is a {Age}, year old {Sex} {Smoker} with a BMI of {BMI} and insurance cost of ${Insurance_cost}."format(name = “Marina”, Age = str(value[“Age”]), Sex = value[“Sex”], Smoker = value[“Smoker”], BMI = str(value[“BMI”]), Insurance_cost = str(value [“Insurance_cost”])))

Also, I don’t understand why the solution uses

record[“Sex”]

and not

key[“Sex”]

Since “Sex” is a value from Key…

thanks for the help

record is the name of the dictionary (or, rather, the variable that holds the dictionary)
so you’d do record["sex"] to access the value that goes with a key of “sex” for that dictionary.

Example:

junk = {
    "amount": 3,
    "salvage": 7.25,
    "comment": "nearly useless"
}

# access the value that goes with the key "amount"
w = junk["amount"]
print(w)
1 Like

Thanks for the explanation!

The problem was I had no period before format a in

.format()