Skip to content

Update lab-python-error-handling.ipynb #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
123 changes: 122 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,128 @@
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"inventory = {}\n",
"\n",
"def initialize_inventory(products, inventory):\n",
" \"\"\"Initialize inventory by asking user input for each product.\"\"\"\n",
" try:\n",
" for product in products:\n",
" response = input(f\"Añado '{product}' al inventario (si/no)? \").strip().lower()\n",
" if response == \"si\":\n",
" inventory[product] = 0\n",
" elif response == \"no\":\n",
" continue\n",
" else:\n",
" raise ValueError(\"Respuesta inválida. Por favor, responde 'si' o 'no'.\")\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
" finally:\n",
" return inventory\n",
"\n",
"products = [\"sandals\", \"shoes\", \"glasses\", \"chandal\", \"sneakers\"]\n",
"inventory = initialize_inventory(products, inventory)\n",
"\n",
"print(\"\\nInventario:\")\n",
"for product in inventory:\n",
" print(product)\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"Prompt user to input customer orders.\"\"\"\n",
" customer_orders = set()\n",
" try:\n",
" while True:\n",
" product_name = input(\"Introduce el nombre del producto: \").strip()\n",
" if product_name == \"\":\n",
" break\n",
" customer_orders.add(product_name)\n",
" except Exception as e:\n",
" print(f\"Error al recibir pedidos del cliente: {e}\")\n",
" else:\n",
" print(\"Pedidos del cliente registrados correctamente.\")\n",
" finally:\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"print(\"Pedidos del cliente:\", customer_orders)\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"Update the inventory based on customer orders.\"\"\"\n",
" try:\n",
" for order in customer_orders:\n",
" if order in inventory:\n",
" print(f\"El producto '{order}' ya está en el inventario.\")\n",
" else:\n",
" inventory[order] = 0\n",
" except Exception as e:\n",
" print(f\"Error al actualizar el inventario: {e}\")\n",
" finally:\n",
" return inventory\n",
"\n",
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"print(\"Inventario actualizado:\", updated_inventory)\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"Calculate statistics about customer orders.\"\"\"\n",
" try:\n",
" total_ordered = len(customer_orders)\n",
" total_products = len(products)\n",
" unique_ordered = len(set(customer_orders) - set(products)) # Orders not in inventory\n",
" if total_products > 0:\n",
" percentage_unique_ordered = (unique_ordered / total_products * 100)\n",
" else:\n",
" raise ZeroDivisionError(\"La lista de productos está vacía. No se puede calcular el porcentaje.\")\n",
" except ZeroDivisionError as e:\n",
" print(f\"Error: {e}\")\n",
" return 0, 0\n",
" finally:\n",
" return total_ordered, percentage_unique_ordered\n",
"\n",
"total_ordered, percentage_unique_ordered = calculate_order_statistics(customer_orders, products)\n",
"print(\"Total de productos únicos ordenados:\", total_ordered)\n",
"print(\"Porcentaje de productos únicos pedidos:\", percentage_unique_ordered)\n",
"\n",
"def print_order_statistics(total_ordered, percentage_unique_ordered):\n",
" \"\"\"Print statistics about customer orders.\"\"\"\n",
" try:\n",
" print(f\"Total de productos únicos ordenados: {total_ordered}\")\n",
" print(f\"Porcentaje de productos únicos ordenados: {percentage_unique_ordered:.2f}%\")\n",
" except Exception as e:\n",
" print(f\"Error al imprimir estadísticas: {e}\")\n",
"\n",
"print_order_statistics(total_ordered, percentage_unique_ordered)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"Print the updated inventory.\"\"\"\n",
" try:\n",
" print(f\"Inventario actualizado: {inventory}\")\n",
" except Exception as e:\n",
" print(f\"Error al imprimir el inventario actualizado: {e}\")\n",
"\n",
"print_updated_inventory(updated_inventory)\n",
"\n",
"def main():\n",
" \"\"\"Main function to execute the inventory program.\"\"\"\n",
" try:\n",
" inventory = {}\n",
" products = [\"sandals\", \"shoes\", \"glasses\", \"chandal\", \"sneakers\"]\n",
"\n",
" inventory = initialize_inventory(products, inventory)\n",
"\n",
" customer_orders = get_customer_orders()\n",
"\n",
" updated_inventory = update_inventory(customer_orders, inventory)\n",
" print_updated_inventory(updated_inventory)\n",
"\n",
" total_ordered, percentage_unique_ordered = calculate_order_statistics(customer_orders, products)\n",
" print(\"Total de productos únicos pedidos:\", total_ordered)\n",
" print(\"Porcentaje de productos únicos pedidos que no están en la lista de productos disponibles:\", percentage_unique_ordered)\n",
"\n",
" except Exception as e:\n",
" print(f\"Se produjo un error inesperado: {e}\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n"
]
}
],
Expand Down