Advertisement
teslariu

mult excep

Nov 30th, 2021
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. lista = [1,2,"Hola"]
  5.  
  6. i = int(input("Ingrese 1, 2 o 3: "))
  7.  
  8. # forma recomendada
  9. try:
  10.     numero = int(lista[i])
  11. except ValueError:
  12.     print(f"lista[{i}] no es un número")
  13. except IndexError:
  14.     print(f"No existe el índice {i}")
  15. else:
  16.     print(f"lista[1]: {lista[i]}")
  17.  
  18. # forma sin distinguir los errores
  19. try:
  20.     numero = int(lista[i])
  21. except (ValueError, IndexError):
  22.     print("Error de valor o de índice")
  23. else:
  24.     print(f"lista[1]: {lista[i]}")
  25.  
  26. # forma no recomendada
  27. try:
  28.     numero = int(lista[i])
  29. except Exception:
  30.     print("Error")
  31. else:
  32.     print(f"lista[1]: {lista[i]}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement