From 8323b6a1dc529123a67333258859ec56e8f6e5fa Mon Sep 17 00:00:00 2001 From: Gema <132750075+GemaVNZ@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:16:19 +0100 Subject: [PATCH 1/3] Se realizan los ejercicios propuestos. --- lab-python-error-handling.ipynb | 157 +++++++++++++++++++++++++++++++- 1 file changed, 153 insertions(+), 4 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..501ff45 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -42,17 +42,166 @@ { "cell_type": "code", "execution_count": null, - "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", + "id": "382b7794", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " \n", + " for product in products:\n", + " while True:\n", + " try: \n", + " cantidad = int(input(f\"Introduce la cantidad de {product}\"))\n", + " if (cantidad > 0):\n", + " inventory[product] = (cantidad)\n", + " print(f\"{product} agregado con éxito\")\n", + " break\n", + " else:\n", + " raise ValueError(\"La cantidad debe ser mayor que 0\")\n", + " except ValueError as e:\n", + " print(f\"Error: {e}. Intenta nuevamente.\")\n", + " finally:\n", + " print(\"Intento de agregar cantidad realizado.\")\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "490be403", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}\n", + "\n", + "def get_customer_orders():\n", + " customer_order = set()\n", + "\n", + " while True:\n", + " try: \n", + " product = input(\"Introduce el nombre del producto que quieres: \").lower() \n", + " if product in inventory and inventory[product] > 0:\n", + " print(f\"{product} en stock\")\n", + " customer_order.add(product)\n", + " else:\n", + " raise ValueError(f\"{product} sin stock\")\n", + " except ValueError as g:\n", + " print(f\"Error : {g}\")\n", + " \n", + " another = input(\"¿Quieres otro producto? (si/no): \").strip().lower()\n", + " if another == \"no\": \n", + " break\n", + " \n", + " return customer_order" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "396611af", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " try:\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " raise ValueError(f\"No hay suficiente stock en el inventario para {product}\")\n", + " except KeyError:\n", + " print(\"Producto no encontrado en el inventario\")\n", + " except TypeError:\n", + " print(\"Error: el inventario o el pedido del cliente tienen un formato incorrecto\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b73cb827", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mug sin stock\n", + "(0, 0.0)\n" + ] + } + ], + "source": [ + "products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "customer_order = get_customer_orders()\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " try:\n", + " total_products = len(customer_orders)\n", + " unique_percentage = (total_products/ len(products)) * 100 \n", + " except ZeroDivisionError:\n", + " print(\"No se han introducido productos\")\n", + " total_products = 0\n", + " unique_percentage = 0\n", + " else: \n", + " print(\"El cálculo de estadísticas se ha completado con éxito\")\n", + " \n", + " return total_products, unique_percentage\n", + "\n", + "print(calculate_order_statistics(customer_order, products))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d9b76120", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " try:\n", + " total_products = order_statistics[0]\n", + " unique_percentage = order_statistics[1]\n", + " except (TypeError, IndexError):\n", + " print (\"Error: La estadística del pedido no es correcta\")\n", + " total_products = 0\n", + " unique_percentage = 0\n", + " else:\n", + " print(\"La estadística del pedido se ha realizado con éxito\")\n", + " \n", + " print(f\"Total products ordered: {total_products}\")\n", + " print(f\"Unique products ordered: {unique_percentage}%\")\n", + "\n", + "print_order_statistics(calculate_order_statistics(customer_order, products))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1927a4a", + "metadata": {}, + "outputs": [], + "source": [ + "def print_update_inventory(inventory):\n", + " print(\"Inventario actualizado: \")\n", + " try:\n", + " for product, quantity in inventory.items():\n", + " print (f\"{product} : {quantity}\")\n", + " except TypeError:\n", + " print(\"Error: el inventario no es válido\")\n", + " finally:\n", + " print(\"El inventario se ha mostrado correctamente\")\n", + " return inventory" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -66,7 +215,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.4" } }, "nbformat": 4, From 8665b6171e42e2e6cf5c16b25e578546b4952f1a Mon Sep 17 00:00:00 2001 From: Gema <132750075+GemaVNZ@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:17:12 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Se=20realizan=20peque=C3=B1as=20modificacio?= =?UTF-8?q?nes.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab-python-error-handling.ipynb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 501ff45..3726b95 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "382b7794", "metadata": {}, "outputs": [], @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "490be403", "metadata": {}, "outputs": [], @@ -100,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "396611af", "metadata": {}, "outputs": [], From 1df8207e365b2e8b82fad8c0aac88312216f4ee6 Mon Sep 17 00:00:00 2001 From: Gema <132750075+GemaVNZ@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:43:28 +0100 Subject: [PATCH 3/3] Se realizan modificaciones. --- .vscode/settings.json | 5 ++++ lab-python-error-handling.ipynb | 46 ++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6c2ff60 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": [ + "master" + ] +} \ No newline at end of file diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3726b95..0ccc879 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "490be403", "metadata": {}, "outputs": [], @@ -83,24 +83,30 @@ " while True:\n", " try: \n", " product = input(\"Introduce el nombre del producto que quieres: \").lower() \n", - " if product in inventory and inventory[product] > 0:\n", - " print(f\"{product} en stock\")\n", - " customer_order.add(product)\n", - " else:\n", - " raise ValueError(f\"{product} sin stock\")\n", + " if product not in inventory:\n", + " raise ValueError(f\"El producto {product} no se encuentra en el inventario\")\n", + " if inventory[product] <= 0:\n", + " raise ValueError (f\"El producto {product} no tiene stock\")\n", + " customer_order.add(product)\n", + " print (f\"El producto {product} se ha agregado a tu pedido\") \n", " except ValueError as g:\n", " print(f\"Error : {g}\")\n", " \n", - " another = input(\"¿Quieres otro producto? (si/no): \").strip().lower()\n", - " if another == \"no\": \n", + " while True:\n", + " another = input(\"¿Quieres otro producto? (si/no): \").strip().lower()\n", + " if another in [\"si\",\"no\"]:\n", " break\n", - " \n", + " else:\n", + " print (\"Por favor, introduce 'si' o 'no'.\")\n", + "\n", + " if another == \"no\":\n", + " break \n", " return customer_order" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "396611af", "metadata": {}, "outputs": [], @@ -108,14 +114,15 @@ "def update_inventory(customer_orders, inventory):\n", " try:\n", " for product in customer_orders:\n", - " if product in inventory and inventory[product] > 0:\n", - " inventory[product] -= 1\n", + " if product in inventory:\n", + " if inventory[product]>0:\n", + " inventory [product]-=1\n", " else:\n", " raise ValueError(f\"No hay suficiente stock en el inventario para {product}\")\n", - " except KeyError:\n", - " print(\"Producto no encontrado en el inventario\")\n", + " except (KeyError, ValueError) as g:\n", + " print(f\"Error: {g}\")\n", " except TypeError:\n", - " print(\"Error: el inventario o el pedido del cliente tienen un formato incorrecto\")\n", + " print(\"Error: el inventario o los pedidos no son válidos\")\n", " return inventory" ] }, @@ -143,13 +150,16 @@ " try:\n", " total_products = len(customer_orders)\n", " unique_percentage = (total_products/ len(products)) * 100 \n", + " if total_products == 0:\n", + " raise ValueError(\"No se han realizado pedidos\")\n", " except ZeroDivisionError:\n", " print(\"No se han introducido productos\")\n", " total_products = 0\n", " unique_percentage = 0\n", - " else: \n", - " print(\"El cálculo de estadísticas se ha completado con éxito\")\n", - " \n", + " except ValueError as g:\n", + " print (f\"Error: {g}\")\n", + " total_products = 0\n", + " unique_percentage = 0\n", " return total_products, unique_percentage\n", "\n", "print(calculate_order_statistics(customer_order, products))"