Why the 'IndexError' exception doesn't handle the error when accessing an index outside the list?

2 weeks ago 19
ARTICLE AD BOX

I'm learning Python and this is the piece of code I'm having trouble with :

taskList = ["a", "b", "c", "d", "e", "f"] def main() : while True : try : indx = int(input("Enter the task index : ")) if (indx - 1) <= len(taskList) and (indx - 1) > 0 : process(indx) except IndexError : print("Out of range") except : print("Input must be a number") else : return def process(indx) : while True : print(f"{indx}. {taskList[indx - 1]}") usrinput = input("Confirm : [Y/n]") if usrinput == "Y" or usrinput == "y" : taskList.pop(indx - 1) print(f"Delete successfull") return elif usrinput == "N" or usrinput == "n" : print("Canceling...") return else : print("the answer should be just y or n") main()

I've simplified it to make it easier to debug without changing the main logic (Sorry I'm still learning), but whenever I access an index outside the list, the program doesn't print("Out of range") and just doesn't display anything. even though i already make the IndexError exception, can anybody help me? i didnt know why my brain can't work with this

(I don't want to rely on AI too much, so I'm doing everything with my own logic, sorry if it's messy)

enter image description here Accessing the correct index
enter image description here Accessing index outside list size

And if you have any suggestions, you can also tell me so I can improve more

Read Entire Article