teslariu

excepciones

May 5th, 2023
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. import math
  6.  
  7. # sin funciones
  8. while True:
  9.    try:
  10.        n = int(input("Ingrese un nro entero: "))
  11.    except ValueError:
  12.        print("Error de valor")
  13.    else:
  14.        print(f"El coseno de {n} es {math.cos(n)}")
  15.        break
  16.        
  17. # con funciones
  18. def ingresar_entero():
  19.    while True:
  20.        try:
  21.            n = int(input("Ingrese un nro entero: "))
  22.        except ValueError:
  23.            print("Error de valor")
  24.        else:
  25.            return n
  26.        
  27. print(f"El coseno de {n} es {math.cos(ingresar_entero())}")
  28.    
  29. """
  30. # Lanzamiento de excepciones:
  31. def suma(a,b):
  32.     """
  33.    Función que retorna la suma de dos nros pasados como argumentos. Si
  34.    se pasan cadenas o listas lanza una excepción de TypeError
  35.    """
  36.     if not isinstance(a,(int,float)) or not isinstance(b,(int,float)):
  37.         raise TypeError("Se requieren dos números")
  38.     return a + b
  39.    
  40. print(suma("h","ola"))
  41.  
Advertisement
Add Comment
Please, Sign In to add comment