Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- lista = [1,2,"Hola"]
- i = int(input("Ingrese 1, 2 o 3: "))
- # forma recomendada
- try:
- numero = int(lista[i])
- except ValueError:
- print(f"lista[{i}] no es un número")
- except IndexError:
- print(f"No existe el índice {i}")
- else:
- print(f"lista[1]: {lista[i]}")
- # forma sin distinguir los errores
- try:
- numero = int(lista[i])
- except (ValueError, IndexError):
- print("Error de valor o de índice")
- else:
- print(f"lista[1]: {lista[i]}")
- # forma no recomendada
- try:
- numero = int(lista[i])
- except Exception:
- print("Error")
- else:
- print(f"lista[1]: {lista[i]}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement