diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..810e812 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,18 +41,122 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Definimos el stock inicial.\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " print('Introduce cantidad de cada producto que tienes en el inventario')\n", + " for producto in products:\n", + " try:\n", + " cantidad = int(input(f\"{producto}: \"))\n", + " if cantidad < 0:\n", + " raise ValueError(\"La cantidad no puede ser negativa.\")\n", + " inventory[producto] = cantidad\n", + " except ValueError as e:\n", + " print(f\"Error: {e}. Por favor, introduce un número entero válido.\")\n", + " return None \n", + " return inventory\n", + "\n", + "# Definimos la funcion coge los pedidos de los clientes.\n", + "def get_customer_orders(products,inventory):\n", + " customer_orders = set()\n", + " while True:\n", + " try:\n", + " pedido = input('Introduce el nombre del producto que quieres pedir: ')\n", + " if pedido in products:\n", + " if inventory[pedido] > 0:\n", + " customer_orders.add(pedido)\n", + " print(f'{pedido} añadido al pedido')\n", + " else:\n", + " print(f'Perdón, no tenemos stock de {pedido}.')\n", + " else:\n", + " print(f'{pedido} no es un producto válido. Selecciona un producto de estos {products}')\n", + " except Exception as e:\n", + " print(f\"Error: {e}. Ha ocurrido un problema al procesar tu pedido.\")\n", + " \n", + " otro_product = input(\"¿Quieres añadir otro producto? (yes/no): \").strip().lower()\n", + " if otro_product != 'yes':\n", + " break\n", + " return customer_orders\n", + "\n", + "# Definimos la función que actualiza el inventario\n", + "def update_inventory(customer_orders, inventory):\n", + " for producto in customer_orders:\n", + " if inventory[producto] > 0:\n", + " inventory[producto] -= 1\n", + " return inventory\n", + "\n", + "# Definimos la función que calcula las estadisticas\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " porcentaje = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, porcentaje\n", + "\n", + "# Definimos la función para imprimir las estadísticas\n", + "def print_order_statistics(order_statistics):\n", + " print('\\nOrder Statistics:')\n", + " print(f'Total Products Ordered: {order_statistics[0]}')\n", + " print(f'Percentage of Products Ordered: {order_statistics[1]}%')\n", + "\n", + "# Definimos la función para imprimir el inventario actualizado\n", + "def print_updated_inventory(inventory):\n", + " print('\\nUpdated Inventory:')\n", + " for producto, cantidad in inventory.items():\n", + " print(f'{producto}: {cantidad}')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e4a54e97", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Introduce cantidad de cada producto que tienes en el inventario\n", + "mug añadido al pedido\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 20.0%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 5\n", + "hat: 7\n", + "book: 8\n", + "keychain: 9\n" + ] + } + ], + "source": [ + " # Iniciamos el inventario\n", + "inventory = initialize_inventory(products)\n", + " \n", + "# Obtenemos los pedidos \n", + "customer_orders = get_customer_orders(products, inventory)\n", + " \n", + "# Calculamos e imprimimos las estadísticas\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + " \n", + "# Actualizamos el inventari0\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -66,7 +170,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.8.2" } }, "nbformat": 4,