Advertisement
teslariu

excepciones

Nov 7th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #raise lanza excepciones KeyError, ValueError, NameError, TypeError
  4.  
  5. def suma(a,b):
  6.     return a+b
  7.  
  8. def resta(a,b):
  9.     return a-b
  10.  
  11. def multiplicar(a,b):
  12.     return a*b
  13.  
  14. def dividir(a,b):
  15.     if b:
  16.         return a/b
  17.     else:
  18.         raise ZeroDivisionError
  19.  
  20. def cargar_datos():
  21.     while True:
  22.         try:
  23.             a = float(input("Ingrese un nro: "))
  24.             return a
  25.         except Exception:
  26.             print("Debe ingresar un nro")
  27.    
  28.  
  29.  
  30. while True:
  31.    
  32.     a = cargar_datos()
  33.     b = cargar_datos()
  34.    
  35.     try:
  36.         print("a+b:",suma(a,b))
  37.         print("a-b:",resta(a,b))
  38.         print("a*b:",multiplicar(a,b))
  39.         print("a/b:",dividir(a,b))
  40.     except ZeroDivisionError:
  41.         print("No se puede dividir por cero")
  42.     finally:
  43.         print("----------------------------------------------")
  44.            
  45.            
  46.     opcion = input("Presione cualquier tecla para continuar ('x' para salir) :")
  47.     if opcion.casefold() == "x":
  48.         print("Adios...")
  49.         break
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement