Skip to content

Latest commit

 

History

History
35 lines (24 loc) · 352 Bytes

File metadata and controls

35 lines (24 loc) · 352 Bytes

KeyError: 'age'

Occurs when accessing a missing key in a dictionary.

Reproduce

data = {
    "name": "john"
}

age = data["age"]
print(age)

Error Message

KeyError: 'age'

Fix

data = {
    "name": "john"
}

age = data.get("age", 0)
print(age)

Reflection

Assumed the key existed without checking.