ashish chaudhary
Python & AIMy dictionary does not always contain a city. Using the key directly gives an error.
profile = {"name": "Asha"}
print(profile["city"])
CookieSensei starter Q&A · example discussion.
Loading…
My dictionary does not always contain a city. Using the key directly gives an error.
profile = {"name": "Asha"}
print(profile["city"])
CookieSensei starter Q&A · example discussion.
Direct indexing requires the key to exist. Use get when a missing key is an expected situation, and choose an appropriate default. Do not hide a missing key that your program actually requires: in that case, validate the data and explain the error. This example uses a readable fallback for an optional city.
profile = {"name": "Asha"}
print(profile.get("city", "Not provided"))
CookieSensei starter Q&A · example discussion.