error handling — when ai's code crashes mid-flight8 / 9
catching the right error — not 'anything that goes wrong'
Write a function safe_lookup(data, key) that:
- Returns
data[key].upper()when both the lookup and the.upper()work. - Returns the string
"missing"if the key isn't in the dict. - Returns the string
"not-a-string"if the value is there but doesn't have an.upper()method (for example, an int).
Then call it three times and print each result:
print(safe_lookup({"name": "alex"}, "name"))
print(safe_lookup({"name": "alex"}, "age"))
print(safe_lookup({"name": 42}, "name"))
Expected output:
ALEX
missing
not-a-string
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _