Diagnose the Failure Type

Medium ⏱ 12 min 48% acceptance ★★★★★ 4.9
Write describe_access(data, key, index) that attempts data[key][index] and returns the value on success, the string 'no such key' on KeyError, 'index out of range' on IndexError, and 'not indexable' on TypeError — three separate except branches.

Examples

Example 1
Input
describe_access({'a': [1, 2]}, 'a', 5)
Output
'index out of range'
Explanation

The list has no index 5.

Example 2
Input
describe_access({'a': 7}, 'a', 0)
Output
'not indexable'
Explanation

Indexing an int raises TypeError.

Constraints

  • Three distinct except clauses.
  • No isinstance pre-checks.

Topics

Exception Handlingmultiple except

Companies

MicrosoftOracleIBM

Hints

Hint 1

Order of except blocks does not matter here — they are disjoint.

Hint 2

Return from inside each branch.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.