As Gary notes in the comments, ValueError is the right choice.
Another rival will be IndexError , as suggested by Wikiii122. However, according to Python docs,
exception IndexError
Raised when the sequence index is out of range. (Slice indexes are silently truncated to fall into the valid range; if the index is not a prime integer, TypeError is raised.)
This would probably be triggered if you didn't bother to raise an exception, but not as descriptive as TypeError, whose documentation looks like this:
exception ValueError
Raised when an inline operation or function receives an argument with the correct type but an invalid value , and the situation is not described by a more specific exception, such as IndexError.
source share