box[key] and reports which error it met, if any.
def probe(box, key):
try:
return box[key]
except (KeyError, IndexError) as e:
return type(e).__name__[0]
except TypeError:
return "T"
print(probe([5, 6], 2), probe({"a": 1}, "b"), probe("hi", "1"), probe((7,), -1))
What does the program print?Log in to join the discussion.
Loading…