I am trying to trigger the IndexError when practicing exceptions

24 Views Asked by At

I have a task I'm attempting to solve, I can't figure why I can't trigger the Index Error exception. The instructions are to print the value at the input index and print the values at the index before and after. When I enter the 4th index as input the entered index and the previous index are printed but there is no exception thrown and nothing printed for the 5th index. Isn't is suppose to be out of bounds because im asking for a non existent value to be printed? Im super new and I feel like im missing something trivial...

numList = [5, 10, 15, 20, 25]
index = int(input('Enter index: '))

try:
   output = numList[index -1 : index +2]
   print(f'{output}')
except IndexError:
    print('out of bounds')
finally:
    print(output)

Tried to list slice and get the index error to be thrown

0

There are 0 best solutions below