There are already some posts that talk about this exception but I did not see in any of them what happens in this code. and it is the following... The code receives an integer from the user and uses it to return the index of a list, but entering a data type str would throw an error. The thing is that I apply the except ValueError, but it never takes it, I get the error in the console instead of the default message. From already thank you very much!
lista_numeros = [6,14,11,3,2,1,15,19]
numero_elejido = int(input("Elija la posicion a mostrar en la lista "))
try:
print(lista_numeros[numero_elejido])
except ValueError:
print("Introduza el numero de forma numerica, no textual")
except IndexError:
print("La posicion elejida es inexistente")
finally:
print("Adios!")
The error occurs because the evaluation of user input
int(input())
is outside the error catcher, that is, outside the blocktry - except
.It should look like this:
Now yes, when entering an alphanumeric the following error will appear: