|
| 1 | +""" |
| 2 | +Excepciones |
| 3 | +""" |
| 4 | + |
| 5 | +try: |
| 6 | + x = 10 / '0' |
| 7 | +except ZeroDivisionError: |
| 8 | + print("Error de división con 0") |
| 9 | +except TypeError as ty: |
| 10 | + print(f"Error de tipo: {ty}") |
| 11 | +else: |
| 12 | + print(x) |
| 13 | +finally: |
| 14 | + print("Aquí termina el manejo de las excepciones") |
| 15 | + |
| 16 | +try: |
| 17 | + print(10/1) |
| 18 | + print([1, 2, 3, 4][4]) |
| 19 | +except Exception as e: |
| 20 | + print(f"Se ha producido un error: {e} ({type(e).__name__})") |
| 21 | + |
| 22 | + |
| 23 | +""" |
| 24 | +Extra |
| 25 | +""" |
| 26 | +### Debe ser entero, positivo y mayor que 0 |
| 27 | +def condition_ver(number): |
| 28 | + if type(number) is float: |
| 29 | + raise Exception(f"No se acepta valores flotantes, [{number}] es un valor flotante") |
| 30 | + if not type(number) is int: |
| 31 | + raise TypeError(f"El tipo de dato debe ser un int, [{number}] no lo es") |
| 32 | + if not number > 0: |
| 33 | + raise ValueError( |
| 34 | + f"El número no puede ser negativo ni 0, [{number}] no cumple con esta condición" |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +def geometric_progression(first_number,reason,position): |
| 40 | + try: |
| 41 | + condition_ver(first_number) |
| 42 | + condition_ver(reason) |
| 43 | + condition_ver(position) |
| 44 | + except TypeError as e: |
| 45 | + print(f"{type(e).__name__}: {e}") |
| 46 | + except ValueError as e: |
| 47 | + print(f"{type(e).__name__}: {e}") |
| 48 | + except Exception as e: |
| 49 | + print(f"{type(e).__name__}: {e}") |
| 50 | + else: |
| 51 | + s = first_number |
| 52 | + for i in range(1,position): |
| 53 | + s *= reason |
| 54 | + print (f"El valor en la {position}º de la progresión geometrica, con valor inicial: {first_number}, y razon: {reason} es: {s}") |
| 55 | + finally: |
| 56 | + print("Ha terminado el programa") |
| 57 | + |
| 58 | + |
| 59 | +# geometric_progression(1,0,3) |
| 60 | + |
| 61 | +class StrTypeError(Exception): |
| 62 | + pass |
| 63 | + |
| 64 | +def process_params(parameters: list): |
| 65 | + |
| 66 | + if len(parameters) < 3: |
| 67 | + raise IndexError() |
| 68 | + elif parameters[1] == 0: |
| 69 | + raise ZeroDivisionError() |
| 70 | + elif type(parameters[2]) == str: |
| 71 | + raise StrTypeError("El tercer elemento no puede ser un cadena de texto") |
| 72 | + |
| 73 | + print(parameters[2]) |
| 74 | + print(parameters[0]/parameters[1]) |
| 75 | + print(parameters[2] + 5) |
| 76 | + |
| 77 | +try: |
| 78 | + process_params([1,2,3,4]) |
| 79 | +except IndexError as e: |
| 80 | + print("El número de elementos de la lista debe ser mayor que dos") |
| 81 | +except ZeroDivisionError as e: |
| 82 | + print("El segundo elemento de la lista no puede ser un cero (0)") |
| 83 | +except StrTypeError as e: |
| 84 | + print(f"{type(e).__name__} : {e} ") |
| 85 | +except Exception as e: |
| 86 | + print(f"Se ha producido un error inesperado: {e} ({type(e).__name__})") |
| 87 | +else: |
| 88 | + print("No se ha producido ni un error") |
| 89 | +finally: |
| 90 | + print("El programa finaliza sin deterse") |
| 91 | + |
0 commit comments