Error and Exception Handling · 12 / 20 Exercise Easy

Safe index

Objective

Create a Python program that shows an element of a fixed list from the position typed by the user, handling positions that do not exist.

Instructions

  • Define the list [10, 20, 30, 40, 50].
  • Read an integer position.
  • Show the element at that position (negative positions count from the end, as always in Python).
  • If the position does not exist, catch IndexError and show Index out of range.
Input
2
Output
30

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise