In calling an item in a list, Python takes 1 as the 2nd of the list, and 0 as the 1st of the list, 3 as the 4th of the list, and so on. Translating the next three lines of the code after the function defined, "check list No.2, check list No. 5, check list No. 3". there is no more than 3 items, so check(4) raises the error. Tweak the definition of check(n), something like the one shown below:
values = ["a", "b", "1"]
def check(n):
try:
print(values[n], 'exists')
except:
print("value doesn\'t exist")
The try keyword just tests if it raises no errors. If it does, it hops on over to except keyword, executing that one. The code above specifies the value item, and the comma would be not belting an error. Try it again, and the output:
b exists
value doesn't exist
1 exists
Be careful though, if you're trying to print out one line of string for one error, don't just type (except:) when it is supposed to be (except name of error:) for every single unique error planned to be in the program. It'll execute only the very first one, and not the rest.