Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 130 additions & 5 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,143 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✅ Lista de débitos generada correctamente.\n",
"Ejecución finalizada (get_debits).\n",
"[(-1200, 'debit')]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def get_debits(transactions):\n",
" try:\n",
" if not isinstance(transactions, list):\n",
" raise TypeError(\"❌ Error: la variable 'transactions' debe ser una lista.\")\n",
" debits = list(filter(lambda x: x[1] == 'debit', transactions))\n",
" if not debits:\n",
" raise ValueError(\"⚠️ No se encontraron transacciones de tipo 'debit'.\")\n",
" except (TypeError, ValueError, IndexError) as e:\n",
" print(f\"Error al obtener los débitos: {e}\")\n",
" debits = []\n",
" else:\n",
" print(\"✅ Lista de débitos generada correctamente.\")\n",
" finally:\n",
" print(\"Ejecución finalizada (get_debits).\")\n",
" return debits\n",
"\n",
"transactions = [(-1200, 'debit'), (2500, 'credit')]\n",
"print(get_debits(transactions))\n",
"\n",
"def sort_debits_descending(debits):\n",
" try:\n",
" if not debits:\n",
" raise ValueError(\"⚠️ La lista de débitos está vacía.\")\n",
" sort_descending = lambda x: x[0]\n",
" debits_sorted = sorted(debits, key=sort_descending, reverse=True)\n",
" except Exception as e:\n",
" print(f\"Error al ordenar débitos: {e}\")\n",
" debits_sorted = []\n",
" else:\n",
" print(\"✅ Débitos ordenados correctamente.\")\n",
" finally:\n",
" print(\"Ejecución finalizada (sort_debits_descending).\")\n",
" return debits_sorted\n",
"\n",
"def apply_interest(balances, rate=0.05):\n",
" try:\n",
" if not all(isinstance(x, (int, float)) for x in balances):\n",
" raise TypeError(\"❌ Todos los elementos deben ser numéricos.\")\n",
" new_balances = list(map(lambda x: x * (1 + rate), balances))\n",
" except TypeError as e:\n",
" print(f\"Error al aplicar interés: {e}\")\n",
" new_balances = []\n",
" else:\n",
" print(\"✅ Interés aplicado correctamente.\")\n",
" finally:\n",
" print(\"Ejecución finalizada (apply_interest).\")\n",
" return new_balances\n",
"\n",
"def update_accounts_interest(accounts):\n",
" try:\n",
" if not isinstance(accounts, list):\n",
" raise TypeError(\"❌ 'accounts' debe ser una lista.\")\n",
" updated_accounts = list(map(\n",
" lambda acc: {'balance': acc['balance'] * (1 + acc['interest_rate'])},\n",
" accounts\n",
" ))\n",
" except KeyError as e:\n",
" print(f\"Error: falta la clave {e} en uno de los diccionarios.\")\n",
" updated_accounts = []\n",
" except TypeError as e:\n",
" print(f\"Error de tipo: {e}\")\n",
" updated_accounts = []\n",
" else:\n",
" print(\"✅ Cuentas actualizadas correctamente.\")\n",
" finally:\n",
" print(\"Ejecución finalizada (update_accounts_interest).\")\n",
" return updated_accounts\n",
"\n",
"from functools import reduce\n",
"\n",
"def total_negative_balances(balances):\n",
" try:\n",
" if not balances:\n",
" raise ValueError(\"⚠️ La lista de balances está vacía.\")\n",
" negative_balances = list(filter(lambda x: x < 0, balances))\n",
" total_negative = reduce(lambda a, b: a + b, negative_balances)\n",
" except TypeError as e:\n",
" print(f\"Error: tipo de dato incorrecto en la lista. {e}\")\n",
" total_negative = 0\n",
" except ValueError as e:\n",
" print(f\"Error de valor: {e}\")\n",
" total_negative = 0\n",
" else:\n",
" print(\"✅ Total de balances negativos calculado correctamente.\")\n",
" finally:\n",
" print(\"Ejecución finalizada (total_negative_balances).\")\n",
" return total_negative\n",
"\n",
"def calculate_balance(account):\n",
" try:\n",
" if 'balance' not in account or 'withdrawals' not in account:\n",
" raise KeyError(\"❌ Falta la clave 'balance' o 'withdrawals'.\")\n",
" if not all(isinstance(w, (int, float)) for w in account['withdrawals']):\n",
" raise TypeError(\"❌ Los retiros deben ser valores numéricos.\")\n",
" total_withdrawals = sum(account['withdrawals'])\n",
" remaining_balance = account['balance'] - total_withdrawals\n",
" except (KeyError, TypeError) as e:\n",
" print(f\"Error al calcular balance: {e}\")\n",
" remaining_balance = None\n",
" else:\n",
" print(f\"✅ Balance calculado correctamente: {remaining_balance}\")\n",
" finally:\n",
" print(\"Ejecución finalizada (calculate_balance).\")\n",
" return remaining_balance\n",
"\n",
"\n",
"def calculate_all_balances(accounts):\n",
" try:\n",
" remaining_balances = list(map(calculate_balance, accounts))\n",
" except Exception as e:\n",
" print(f\"Error general al procesar cuentas: {e}\")\n",
" remaining_balances = []\n",
" finally:\n",
" print(\"Ejecución finalizada (calculate_all_balances).\")\n",
" return remaining_balances\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +191,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down