From a71c2bc3e33dc26bbde7fdedd79a12aaf40b4869 Mon Sep 17 00:00:00 2001 From: Pol-1992 Date: Wed, 21 May 2025 21:17:08 +0200 Subject: [PATCH] solved_lab --- lab-python-error-handling.ipynb | 184 +++++++++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..57d79f3 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,191 @@ "\n", "4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "9d56b7fa", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "4b6ca543", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Indica la catidad {product}s disponible: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Cantidad negativa, ingresa un valor positivo\")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "c08703f1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: ''\n", + "Error: invalid literal for int() with base 10: 't'\n", + "Error: Cantidad negativa, ingresa un valor positivo\n", + "Stock: {'t-shirt': 2, 'mug': 3, 'hat': 4, 'book': 5, 'keychain': 7}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "print(f\"Stock: {inventory}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6901ad27", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = products" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "41bcd979", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " prices = {}\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Ingresa el precio de {product}: \"))\n", + " if price >= 0:\n", + " prices[product] = price\n", + " valid_input = True\n", + " else:\n", + " print(\"Cantidad negativa, ingresa un valor positivo.\")\n", + " except ValueError:\n", + " print(\"Caracter invalido, ingresa un valor positivo.\")\n", + " \n", + " return prices" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ea3d033f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cantidad negativa, ingresa un valor positivo.\n", + "Caracter invalido, ingresa un valor positivo.\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 3.6, 'mug': 6.0, 'hat': 8.0, 'book': 7.0, 'keychain': 8.0}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_total_price(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "06cfa4c1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"¿Cuántos productos quiere pedir el cliente?: \"))\n", + " if num_orders < 0:\n", + " print(\"Cantidad negativa, ingresa un valor positivo.\")\n", + " else:\n", + " break\n", + " except ValueError:\n", + " print(\"Caracter invalido, ingresa un valor positivo.\")\n", + "\n", + " customer_orders = []\n", + "\n", + " for _ in range(num_orders):\n", + " while True:\n", + " product = input(\"Ingresa el nombre del producto que quieres pedir: \").strip().lower()\n", + " if product not in inventory:\n", + " print(\"Producto no encontrado en el inventario.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"Producto sin stock disponible.\")\n", + " else:\n", + " customer_orders.append(product)\n", + " inventory[product] -= 1 \n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "f90de2b8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Caracter invalido, ingresa un valor positivo.\n", + "Producto sin stock disponible.\n", + "Producto no encontrado en el inventario.\n", + "Producto no encontrado en el inventario.\n", + "Producto sin stock disponible.\n", + "Pedido: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Stock: {'t-shirt': 1, 'mug': 0, 'hat': 1, 'book': 3, 'keychain': 2}\n" + ] + } + ], + "source": [ + "get_customer_orders(inventory)\n", + "print(f\"Pedido: {customer_orders}\")\n", + "print(f\"Stock: {inventory}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +270,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,