From 2f16f8714ba13f283d47056b7622ed4b8eb7cf2d Mon Sep 17 00:00:00 2001 From: Julie Tring Date: Fri, 20 Jun 2025 16:31:09 +0200 Subject: [PATCH] lab complete --- lab-python-error-handling.ipynb | 200 +++++++++++++++++++++++++++++++- 1 file changed, 197 insertions(+), 3 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..375f94b 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,207 @@ "\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": 13, + "id": "a7e390fa-6a92-4208-9839-dddb139b9104", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 1\n", + "Enter the quantity of mugs available: 1\n", + "Enter the quantity of hats available: 1\n", + "Enter the quantity of books available: 1\n", + "Enter the quantity of keychains available: 1\n", + "Enter the number of customer orders: one\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'one'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Number of orders cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 1\n", + "Enter the name of one of the product : mugg\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: this product is not available. Please enter one that is.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of one of the product : mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 1.\n", + "Percentage of Products Ordered: 20.00%. \n", + "\n", + "Updated Inventory: \n", + "t-shirt : 1\n", + "hat : 1\n", + "book : 1\n", + "keychain : 1\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug : 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price : 10\n" + ] + } + ], + "source": [ + "list_products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def initialize_inventory ():\n", + " #inventory = {product : int(input(f\"Enter the quantity of {product} available: \")) for product in list_products}\n", + " inventory = {}\n", + " for product in list_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", + "def get_customer_orders(inventory):\n", + " customer_orders = []\n", + " while True:\n", + " try:\n", + " customer_status=int(input(\"Enter the number of customer orders:\"))\n", + " if customer_status < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " for i in range (customer_status):\n", + " while True:\n", + " product = input(f\"Enter the name of one of the product :\")\n", + " if product not in inventory:\n", + " print(\"Error: this product is not available. Please enter one that is.\")\n", + " elif inventory[product] <=0 :\n", + " print(f\"Error: {product} out of stock.\")\n", + " else:\n", + " customer_orders.append(product)\n", + " break\n", + " return customer_orders\n", + " \n", + "def calculate_total_price(customer_orders):\n", + " prices = {}\n", + " #prices = {product : round(float(input(f\"Enter the price of {product} : \")),1) for product in customer_orders }\n", + " for product in customer_orders :\n", + " valid_price = False\n", + " while not valid_price:\n", + " try: \n", + " price = int(input(f\"Enter the price of {product} : \"))\n", + " if price < 0 or not float(price):\n", + " raise ValueError(\"Invalid price ! Please enter a non-negative value in numbers.\")\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " prices[product] = price \n", + " print(f\"Total Price : {sum(prices.values())}\")\n", + " return prices\n", + " \n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = { product: (amount -1 if product in customer_orders else amount) for product, amount in inventory.items() if (product not in customer_orders and amount > 0) or\n", + " (product in customer_orders and amount > 1)} \n", + " return inventory\n", + "\n", + "def calculate_order_statistics (customer_orders,list_products,inventory) :\n", + " order_statistics = {}\n", + " total_prod_ordered = len(customer_orders)\n", + " order_statistics[\"total_prod_ordered\"] = total_prod_ordered\n", + " \n", + " total_inventory = len(list_products)\n", + " percentage_products_ordered = (total_prod_ordered / total_inventory)*100\n", + " order_statistics [\"percentage_products_ordered\"] = percentage_products_ordered\n", + " return order_statistics\n", + "\n", + "def print_order_statistics (order_statistics):\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {order_statistics[\"total_prod_ordered\"]}.\")\n", + " print(f\"Percentage of Products Ordered: {order_statistics[\"percentage_products_ordered\"]:.2f}%. \")\n", + "\n", + "def print_updated_inventory (inventory) :\n", + " print(f\"Updated Inventory: \")\n", + " for product, quantity in inventory.items():\n", + " print (f\"{product} : {quantity}\")\n", + "\n", + "inventory = initialize_inventory ()\n", + "customer_orders = get_customer_orders(inventory)\n", + "print()\n", + "order_statistics = calculate_order_statistics (customer_orders,list_products,inventory)\n", + "print_order_statistics (order_statistics)\n", + "print()\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print_updated_inventory (inventory)\n", + "prices = calculate_total_price(customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0fc3541e-14b0-4d36-87a2-745d1f7cbefe", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +284,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,