From 5892ec47634c02fe65df459d708c287e2ce9e608 Mon Sep 17 00:00:00 2001 From: FranBretones Date: Thu, 3 Apr 2025 21:38:47 +0200 Subject: [PATCH] lab Solved --- lab-python-error-handling.ipynb | 168 +++++++++++++++++++++++++++++++- 1 file changed, 163 insertions(+), 5 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..5077353 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,18 +41,176 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity cannot be negative. Please enter a valid quantity.\n", + "Inventory initialized successfully.\n", + "Current Inventory:\n", + "t-shirt: 100\n", + "mug: 100\n", + "hat: 100\n", + "book: 100\n", + "keychain: 100\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity >= 0: #only accept positive number \n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\") \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\") \n", + " return inventory\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #products for inventory\n", + "\n", + "inventory = initialize_inventory(products)\n", + "print(\"Inventory initialized successfully.\")\n", + "print(\"Current Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "db40e656", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Only 47 mugs are available.\n", + "Order completed!\n" + ] + } + ], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_order = {} #empty order\n", + " while True:\n", + " try:\n", + " product_name = input(\"Enter the desired product: \").strip().lower()\n", + " if product_name not in inventory: #if user type an invalid product \n", + " raise ValueError(f\"{product_name} is not available in the inventory.\")\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(f\"{product_name} is out of stock.\")\n", + " \n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product_name} you want to order: \"))\n", + " if quantity <= 0:\n", + " raise ValueError(\"Quantity must be a positive number.\")\n", + " if quantity > inventory[product_name]:\n", + " raise ValueError(f\"Only {inventory[product_name]} {product_name}s are available.\")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " \n", + " customer_order[product_name] = quantity\n", + " inventory[product_name] -= quantity # Update inventory\n", + " \n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " \n", + " another_product = input(\"Do you want to order more products? (yes/no): \").strip().lower()\n", + " if another_product != 'yes':\n", + " print(\"Order completed!\")\n", + " break\n", + " return customer_order\n", + "\n", + "customer_order = get_customer_orders(inventory) #get customer orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d242be11", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mugis not available\n", + "None\n" + ] + } + ], + "source": [ + "\n", + "def update_inventory(customer_order, inventory):\n", + " for product in customer_order:\n", + " try:\n", + " if product in inventory:\n", + " if inventory[product]>0:\n", + " inventory[product]-=1\n", + " else:\n", + " print(f\"{product} is not available\")\n", + " else:\n", + " raise KeyError(f\"{product} doen't exist in this inventory\")\n", + " except KeyError as e:\n", + " print(e)\n", + " \n", + "\n", + "print(update_inventory(customer_order, inventory))" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "3840e09e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 20.00%\n" + ] + } + ], "source": [ - "# your code goes here" + "def calculate_order_statistics(customer_order, products):\n", + " total_products_ordered = len(customer_order)\n", + " total_products = 0 \n", + " try:\n", + " total_products_ordered = len(customer_order)\n", + " percentage_products_ordered = (total_products_ordered / len(products) * 100)\n", + " return total_products_ordered, percentage_products_ordered\n", + " except ZeroDivisionError: \n", + " print(f\"Error: the list can't be empty\")\n", + " \n", + "result = calculate_order_statistics(customer_order, products)\n", + "# Print the order statistics\n", + "print(f\"Order Statistics:\\nTotal Products Ordered: {result[0]}\\nPercentage of Products Ordered: {result[1]:.2f}%\")\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "venv", "language": "python", "name": "python3" }, @@ -66,7 +224,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.1" } }, "nbformat": 4,