From d81e46665680e6f6c997f086b10cf8902d3339d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Ruiz=20Bellido?= Date: Thu, 15 Jan 2026 18:27:04 +0100 Subject: [PATCH] resolved --- lab-python-error-handling.ipynb | 233 +++++++++++++++++++++++++++++++- 1 file changed, 231 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..12a90eb 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,240 @@ "\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": 25, + "id": "9368fda7", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "b01eccc4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 30, 'mug': 0, 'hat': 0, 'book': 40, 'keychain': 0}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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\n", + "\n", + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "6afa7880", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 4.\n", + "Error: No stock available for mug. Please, try again\n", + "Enter the name of the product the customer wants to order: keychain\n", + "Enter the name of the product the customer wants to order: t-shirt\n", + "Enter the name of the product the customer wants to order: hat\n" + ] + } + ], + "source": [ + "def get_customer_orders(products, inventory):\n", + " \"\"\"It prompts the user to enter the number of customer orders and gathers the product names using \n", + " a loop and user input with comprehension.\"\"\"\n", + " valid_orders = False\n", + " while not valid_orders:\n", + " try:\n", + " number_of_orders = int(input('Enter the number of customer orders: ').strip())\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"The number of orders needs to be >= 0.\")\n", + " \n", + " valid_orders = True\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " print(f'Enter the number of customer orders: {number_of_orders}.')\n", + "\n", + " customer_orders = set()\n", + " \n", + " for n in range (0, number_of_orders):\n", + " while True:\n", + " try:\n", + " product = input(f'Enter the name of a product from {products}').strip()\n", + " if product not in inventory:\n", + " raise ValueError(f'Choose betweeen: {list(inventory.keys())}')\n", + " \n", + " if inventory[product] <= 0:\n", + " raise ValueError(f'No stock available for {product}')\n", + " customer_orders.add(product)\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Error: {e}. Please, try again\")\n", + "\n", + " for x in customer_orders:\n", + " print(f'Enter the name of the product the customer wants to order: {x}')\n", + "\n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders(products, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "d3f0cb79", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: <3>.\n", + "Percentage of Products Ordered: <60.0>%\n" + ] + } + ], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"It takes customer_orders and products and it calculates the order statistics.\"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_prodcuts_ordered = total_products_ordered/len(products)*100\n", + " return (total_products_ordered, percentage_of_prodcuts_ordered)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"It takes `order_statistics` as a parameter and it prints the order statistics.\"\"\"\n", + " print(\n", + " f'Order Statistics:\\n'\n", + " f'Total Products Ordered: <{order_statistics[0]}>.\\n'\n", + " f'Percentage of Products Ordered: <{order_statistics[1]}>%'\n", + " )\n", + " \n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "7376826d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 49, 'hat': 49, 'book': 50, 'keychain': 49}" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"It takes customer_orders and inventory and it updates the inventory dictionary based on the customer orders\n", + " and it removes the product from the inventory if its quantity becomes zero after fulfilling the customer orders \n", + " using comprehension to filter out the products with a quantity of zero from the inventory.\"\"\"\n", + " # for, if, else new_dict = {key: (operation1 if condition is True else operation2), for key, value in dict.items()}\n", + " updated_inventory = {\n", + " product: (value - 1 if product in customer_orders else value)\n", + " for product, value in inventory.items()\n", + " }\n", + " updated_inventory = {product: value for product, value in updated_inventory.items() if value != 0}\n", + " return updated_inventory\n", + "\n", + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "d726d89c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total price: 60.0€\n" + ] + }, + { + "data": { + "text/plain": [ + "60.0" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_total_price(products, customer_orders):\n", + "\n", + " prices = {}\n", + "\n", + " valid_price = False\n", + "\n", + " for product in customer_orders:\n", + "\n", + " while True:\n", + "\n", + " try:\n", + " price = float(input(f'Enter the price of {product}: '))\n", + "\n", + " if price < 0:\n", + " raise ValueError(\"It needs to be a positive number\")\n", + " \n", + " prices[product] = price\n", + " break\n", + " except ValueError as e:\n", + " print(f'Error: {e}')\n", + "\n", + " total_price = sum(prices.values())\n", + "\n", + " print(f'Total price: {total_price}€')\n", + "\n", + " return total_price\n", + "\n", + "calculate_total_price(products, customer_orders)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +319,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,