promptdojo_

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

full-screen editor opens — close anytime to keep reading.