Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- Script que pide dos numeros y devuelve su cociente
- def ingresar(n):
- while True:
- try:
- numero = float(input(f"Ingrese el {n}º nro: "))
- except ValueError:
- print("Error, debe ingresar un número")
- else:
- return numero
- def dividir(a,b):
- if b:
- return f"{a}/{b} = {a/b}"
- else:
- return "No se puede dividir por cero"
- a = ingresar(1)
- b = ingresar(2)
- print(dividir(a,b))
- Pseudocódigo
- 1)
- try:
- descargar una pagina web
- except NoExisteLaPaginaError:
- print("No existe la página")
- except ConexionRechazadaError:
- print("Conexiòn rechazada")
- except ServidorNoRespondeError:
- print("Servidor no responde")
- 2)
- try:
- algo
- except (error1, error2, error3):
- print("Error 1, 2 o 3")
- 3)
- try:
- algo
- otra cosa # conviene una UNICA instruccion dentro de try
- except Exception:
- print("Error") # captura cualquier error
- """
- # manejo de excepciones dentro de una función
- def sumar(a,b):
- """
- Función que recibe dos números como argumentos y devuelve
- su suma. Lanza una excepción si los argumentos no son numéricos
- """
- if not isinstance(a,(int,float)) or not isinstance(b,(int,float)):
- raise TypeError("Los argumentos deben ser numéricos")
- return a + b
- print(sumar(25,25.3))
- print(sumar("HO","LA"))
- print(sumar([2,3],[7,8]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement