Catching the right error — not 'anything that goes wrong' — step 8 of 9
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.read, then continue.
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
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.