Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
- Type "help", "copyright", "credits" or "license" for more information.
- >>> # excepciones: son un modelo de manejo de errores
- >>> 25/0
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ZeroDivisionError: division by zero
- >>> 25 + "er"
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: unsupported operand type(s) for +: 'int' and 'str'
- >>> int("er")
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: invalid literal for int() with base 10: 'er'
- >>> lista = [1,2]
- >>> lista[2]
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- IndexError: list index out of range
- >>> dicc = {"red":"rojo", "blue":"azul"}
- >>> dicc["yellow"]
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- KeyError: 'yellow'
- >>>
- # Script que pide dos numeros y devuelve su cociente
- a = float(input("Dividendo: "))
- b = float(input("Divisor: "))
- try:
- print(f"{a}/{b} = {a/b}")
- except ZeroDivisionError:
- print("No se puede dividir por cero")
- else:
- print("Que suerte, no hubo errores")
- finally:
- print("Adios") # esto se ejecuta siempre
- try:
- n = 2 / int("0")
- except ValueError:
- print("Error de valor: debe ingresar un numero")
- except ZeroDivisionError:
- print("Error de division por cero")
- else:
- print(n)
- # otro enfoque
- try:
- n = 2 / int("sdfdsf")
- except (ValueError, ZeroDivisionError):
- print("Error de valor o de division por cero")
- else:
- print(n)
- """
- def sumar(a,b):
- """Función que recibe como argumentos dos numeros y devuelve su suma"""
- if not isinstance(a,(int,float)) or not isinstance(b,(int,float)):
- raise TypeError("Se requieren dos números")
- return a + b
- print(sumar(1,2))
- print(sumar("Ale","jandro"))
- print(sumar([1,2,3],["hola","chau"]))
Advertisement
Add Comment
Please, Sign In to add comment