From 172b388755918943dd3053da3b54ed5df1b8fafc Mon Sep 17 00:00:00 2001 From: Erove13 Date: Thu, 13 Mar 2025 13:59:08 +0100 Subject: [PATCH] Lab Solved --- lab-python-error-handling.ipynb | 70 ++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..dc0612e 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -46,7 +46,75 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " try:\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " user_quantity = int(input(f\"Introduce the quantity of {product}:\"))\n", + " if user_quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " inventory[product] = user_quantity\n", + " break # exit the loop if the input is valid\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " continue # prompt the user for input again\n", + " except Exception as e:\n", + " print(f\"An unexpected error occurred while initializing inventory: {e}\")\n", + " return inventory\n", + "\n", + "def get_customer_orders(products, inventory):\n", + " customer_orders = set()\n", + "\n", + " try:\n", + " while True:\n", + " orders = input('Please enter the name of a product: ').lower()\n", + " if orders in products and inventory.get(orders, 0) > 0:\n", + " customer_orders.add(orders)\n", + " else:\n", + " print(f\"That product doesn't exist or is out of stock. Choose another one from {products}\")\n", + "\n", + " more_orders = input('Would you like to add another product? (Y/N): ').strip().upper()\n", + " if more_orders != 'Y':\n", + " break\n", + " except Exception as e:\n", + " print(f\"An unexpected error occurred while getting customer orders: {e}\")\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " try:\n", + " for product in customer_orders:\n", + " if inventory.get(product, 0) > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"There is no stock for {product}.\")\n", + " except KeyError as e:\n", + " print(f\"Error: {e}. The product does not exist in the inventory.\")\n", + " except Exception as e:\n", + " print(f\"An unexpected error occurred while updating the inventory: {e}\")\n", + " return inventory\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " try:\n", + " # Total Products Ordered\n", + " total_products = len(customer_orders)\n", + "\n", + " # Percentage of Products Ordered\n", + " if len(products) > 0: # Avoid division by zero\n", + " percentage_products = (total_products / len(products)) * 100\n", + " else:\n", + " percentage_products = 0 # If no products, set to 0%\n", + "\n", + " order_statistics = total_products, percentage_products\n", + " except ZeroDivisionError:\n", + " print(\"Error: No products available to calculate the percentage.\")\n", + " order_statistics = 0, 0\n", + " except Exception as e:\n", + " print(f\"An unexpected error occurred while calculating order statistics: {e}\")\n", + " order_statistics = 0, 0\n", + " return order_statistics\n" ] } ],