teslariu

excepciones

Feb 10th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Crear un programa que solicite dos numeros en consola e imprima el
  6. resultado de las cuatro operaciones básicas
  7. >>> Escribir un nro: hola
  8. >>> Error: no ha ingresado un nro.
  9. >>> Escribir un nro: 7
  10. >>> Escribir otro nro: 5
  11. >>> 7 + 5 = 12
  12. >>> 7 - 5 = 2
  13. >>> 7 * 5 = 35
  14. >>> 7 / 5 = 1.4
  15. NOTA: tener en cuenta los errores y no usar condicionales para tratar los
  16. errores
  17. """
  18. while True:
  19.     # valido el ingreso de un nro
  20.     while True:
  21.         try:
  22.             a = float(input("Ingrese un nro: "))
  23.         except ValueError:
  24.             print("Error: no ha ingresado un numero")
  25.         else:
  26.             break
  27.    
  28.     # valido el ingreso de otro nro
  29.     while True:
  30.         try:
  31.             b = float(input("Ingrese otro nro: "))
  32.         except ValueError:
  33.             print("Error: no es un numero entero")
  34.         else:
  35.             break
  36.            
  37.     print(f"{a} + {b} = {a+b}")
  38.     print(f"{a} - {b} = {a-b}")
  39.     print(f"{a} * {b} = {a*b}")
  40.    
  41.     try:
  42.         print(f"{a} / {b} = {a/b}")
  43.     except ZeroDivisionError:
  44.         print(f"{a} / {b} : No se puede dividir por cero")
  45.        
  46.    
  47.     opcion = input("\nPresione cualquier tecla para continuar, 'x' para salir: ")
  48.     if opcion.casefold() == "x":
  49.         print("Adios....")
  50.         break
Advertisement
Add Comment
Please, Sign In to add comment