diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..f556211 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,213 @@ "\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": 7, + "id": "c849fa44", + "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\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\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": 8, + "id": "89d035c2", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory,products):\n", + " \n", + " while True:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the total number of orders: \"))\n", + " if number_of_orders < 1:\n", + " raise ValueError (\"The number of orders must be at least 1.\")\n", + " break\n", + " except ValueError as e :\n", + " print(f\"Invalid input. {e}\")\n", + "\n", + " \n", + " customer_orders = {}\n", + "\n", + " for i in range (number_of_orders):\n", + " while True:\n", + " try:\n", + " product = input(f\"Enter the name of product {i+1} that a customer wants to order: \")\n", + " if product not in products:\n", + " print (f\"Error.{product} is not in available products.\")\n", + " continue \n", + "\n", + " quantity = int(input(f\"Enter the quantity for product {i+1}: \"))\n", + " if quantity < 1:\n", + " raise ValueError (\"The quantity must be at least 1.\")\n", + "\n", + " if product in inventory and inventory[product] >= quantity:\n", + " customer_orders[product] = quantity\n", + " inventory[product] -= quantity\n", + " else:\n", + " print(f\"Error. There is not enough {product} in stock.\") \n", + " except ValueError as e:\n", + " print(f\"Invalid input. {e}\")\n", + " break \n", + "\n", + " return customer_orders \n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "77c1f0da", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product, quantity in customer_orders.items(): \n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0 or price == \"\":\n", + " raise ValueError (\"The price must be un greater than or equal to 0\")\n", + " total_price += price * quantity\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Invalid input. {e}\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "180078bd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total of the products: ['laptop', 'tablet', 'tv']\n", + "Inventory: {'laptop': 5, 'tablet': 5, 'tv': 5}\n", + "Invalid input. The number of orders must be at least 1.\n", + "Error.book is not in available products.\n", + "Error. There is not enough tablet in stock.\n", + "Number of customer orders: 0\n", + "Customer orders: {}\n", + "Total price for the customer order: 0\n" + ] + } + ], + "source": [ + "# Se inicializan los productos disponibles \n", + "products = [\"laptop\", \"tablet\", \"tv\"]\n", + "print(f\"Total of the products: {products}\")\n", + "\n", + "# Se inicializa el inventario con los productos\n", + "inventory = initialize_inventory(products)\n", + "print (f\"Inventory: {inventory}\")\n", + "\n", + "# Se recoge los productos que se van a pedir \n", + "orders = get_customer_orders(inventory,products)\n", + "print(f\"Number of customer orders: {len(orders)}\")\n", + "print(f\"Customer orders: {orders}\")\n", + "\n", + "# Se calcula el precio total de los pedidos\n", + "total_price = calculate_total_price(orders)\n", + "print(f\"Total price for the customer order: {total_price}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "3287d1f1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total of the products: ['laptop', 'tablet', 'tv']\n", + "Inventory: {'laptop': 5, 'tablet': 5, 'tv': 5}\n", + "Number of customer orders: 1\n", + "Customer orders: {'tv': 1}\n", + "Total price for the customer order: 150.0\n" + ] + } + ], + "source": [ + "# Se inicializan los productos disponibles \n", + "products = [\"laptop\", \"tablet\", \"tv\"]\n", + "print(f\"Total of the products: {products}\")\n", + "\n", + "# Se inicializa el inventario con los productos\n", + "inventory = initialize_inventory(products)\n", + "print (f\"Inventory: {inventory}\")\n", + "\n", + "# Se recoge los productos que se van a pedir \n", + "orders = get_customer_orders(inventory,products)\n", + "print(f\"Number of customer orders: {len(orders)}\")\n", + "print(f\"Customer orders: {orders}\")\n", + "\n", + "# Se calcula el precio total de los pedidos\n", + "total_price = calculate_total_price(orders)\n", + "print(f\"Total price for the customer order: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "0e5c39ee", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total of the products: ['laptop', 'tablet', 'tv']\n", + "Inventory: {'laptop': 5, 'tablet': 5, 'tv': 5}\n", + "Number of customer orders: 1\n", + "Customer orders: {'laptop': 2}\n", + "Invalid input. could not convert string to float: 'doscientos'\n", + "Total price for the customer order: 400.0\n" + ] + } + ], + "source": [ + "# Se inicializan los productos disponibles \n", + "products = [\"laptop\", \"tablet\", \"tv\"]\n", + "print(f\"Total of the products: {products}\")\n", + "\n", + "# Se inicializa el inventario con los productos\n", + "inventory = initialize_inventory(products)\n", + "print (f\"Inventory: {inventory}\")\n", + "\n", + "# Se recoge los productos que se van a pedir \n", + "orders = get_customer_orders(inventory,products)\n", + "print(f\"Number of customer orders: {len(orders)}\")\n", + "print(f\"Customer orders: {orders}\")\n", + "\n", + "# Se calcula el precio total de los pedidos\n", + "total_price = calculate_total_price(orders)\n", + "print(f\"Total price for the customer order: {total_price}\")" + ] } ], "metadata": { @@ -90,7 +297,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.4" } }, "nbformat": 4,