Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Script que divide dos numeros
- def validar_numero(n):
- while True:
- try:
- a = float(input(f"Ingrese un nro {n}: "))
- except ValueError:
- print("Error: no ha ingresado un nro")
- else:
- return a
- def dividir(a,b):
- try:
- cociente = a / b
- except ZeroDivisionError:
- return "No se puede dividir por cero..."
- except IOError:
- return "Error de lectura de datos"
- else:
- return f"El cociente entre {a} y {b} es {a/b}"
- a = validar_numero("a")
- b = validar_numero("b")
- print(dividir(a,b))
- # Lanzar excepciones
- def sumar(a,b):
- """Función que implementa la suma de dos números.
- Lanzará una excepción si los parámetros no son del tipo correcto"""
- if not isinstance(a,(float,int)) or not isinstance(b,(float,int)):
- raise TypeError("Se requieren dos números como parámetros")
- return a+b
- print(sumar(2,3.2))
- print(sumar("Hola","Chau"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement