From f5fc1f1fe55af8f2d891a75a2b2d3f3d46b11e55 Mon Sep 17 00:00:00 2001 From: Orgo4ever <163662002+Orgo4ever@users.noreply.github.com> Date: Fri, 19 Jun 2026 14:00:03 +0200 Subject: [PATCH 1/2] Solved lab Solved lab --- lab-python-error-handling.ipynb | 125 +++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..7c15998 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,132 @@ "\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": 1, + "id": "c169f7e4", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 1: products list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6301ba90", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 2: initialize_inventory function\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " inventory[product] = 0\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d87d0756", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 3: get_customer_orders function\n", + "def get_customer_orders(inventory):\n", + " customer_orders = {}\n", + " for product in inventory:\n", + " while True:\n", + " try:\n", + " order_quantity = int(input(f\"Enter the quantity of {product} ordered: \"))\n", + " if order_quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " customer_orders[product] = order_quantity\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Invalid input: {e}. Please enter a valid integer.\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "324c1f58", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 4: calculate_total_price function\n", + "def calculate_total_price(customer_orders):\n", + " prices = {\n", + " \"t-shirt\": 20.0,\n", + " \"mug\": 10.0,\n", + " \"hat\": 15.0,\n", + " \"book\": 25.0,\n", + " \"keychain\": 5.0\n", + " }\n", + " total_price = 0.0\n", + " for product, quantity in customer_orders.items():\n", + " total_price += prices[product] * quantity\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "ab01adc6", + "metadata": {}, + "outputs": [], + "source": [ + "# Cell 5: update_inventory function\n", + "def update_inventory(customer_orders, inventory):\n", + " for product, quantity in customer_orders.items():\n", + " inventory[product] += quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1f005003", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer orders: {'t-shirt': 7, 'mug': 7, 'hat': 7, 'book': 7, 'keychain': 7}\n", + "Total price: 525.0\n", + "Updated inventory: {'t-shirt': 7, 'mug': 7, 'hat': 7, 'book': 7, 'keychain': 7}\n" + ] + } + ], + "source": [ + "# Cell 6: run the program\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "total_price = calculate_total_price(customer_orders)\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "print(\"Customer orders:\", customer_orders)\n", + "print(\"Total price:\", total_price)\n", + "print(\"Updated inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "672ea904", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +211,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.10" } }, "nbformat": 4, From d35d7c6d910ede52f6b2ea822aa822b2438fcf2c Mon Sep 17 00:00:00 2001 From: Orgo4ever <163662002+Orgo4ever@users.noreply.github.com> Date: Fri, 19 Jun 2026 14:11:40 +0200 Subject: [PATCH 2/2] Update lab-python-error-handling.ipynb Adjusted for error handling. --- lab-python-error-handling.ipynb | 112 +++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 24 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 7c15998..a68a8e1 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -94,8 +94,23 @@ "# Cell 2: initialize_inventory function\n", "def initialize_inventory(products):\n", " inventory = {}\n", + "\n", " for product in products:\n", - " inventory[product] = 0\n", + " valid_input = False\n", + "\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + "\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + "\n", " return inventory" ] }, @@ -109,16 +124,54 @@ "# Cell 3: get_customer_orders function\n", "def get_customer_orders(inventory):\n", " customer_orders = {}\n", - " for product in inventory:\n", - " while True:\n", - " try:\n", - " order_quantity = int(input(f\"Enter the quantity of {product} ordered: \"))\n", - " if order_quantity < 0:\n", - " raise ValueError(\"Quantity cannot be negative.\")\n", - " customer_orders[product] = order_quantity\n", - " break\n", - " except ValueError as e:\n", - " print(f\"Invalid input: {e}. Please enter a valid integer.\")\n", + "\n", + " valid_input = False\n", + "\n", + " while not valid_input:\n", + " try:\n", + " number_of_orders = int(input(\"How many different products would the customer like to order? \"))\n", + "\n", + " if number_of_orders > 0:\n", + " valid_input = True\n", + " else:\n", + " print(\"Number of orders must be greater than zero.\")\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + "\n", + " for i in range(number_of_orders):\n", + " valid_product = False\n", + "\n", + " while not valid_product:\n", + " product_order = input(\"Enter the name of a product: \").lower()\n", + "\n", + " if product_order not in inventory:\n", + " print(\"Error: That product is not in the inventory.\")\n", + "\n", + " elif inventory[product_order] <= 0:\n", + " print(\"Error: That product is out of stock.\")\n", + "\n", + " else:\n", + " valid_quantity = False\n", + "\n", + " while not valid_quantity:\n", + " try:\n", + " order_quantity = int(input(f\"Enter the quantity of {product_order} ordered: \"))\n", + "\n", + " if order_quantity <= 0:\n", + " print(\"Order quantity must be greater than zero.\")\n", + "\n", + " elif order_quantity > inventory[product_order]:\n", + " print(f\"Not enough stock. Only {inventory[product_order]} available.\")\n", + "\n", + " else:\n", + " customer_orders[product_order] = order_quantity\n", + " valid_quantity = True\n", + " valid_product = True\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + "\n", " return customer_orders" ] }, @@ -131,16 +184,24 @@ "source": [ "# Cell 4: calculate_total_price function\n", "def calculate_total_price(customer_orders):\n", - " prices = {\n", - " \"t-shirt\": 20.0,\n", - " \"mug\": 10.0,\n", - " \"hat\": 15.0,\n", - " \"book\": 25.0,\n", - " \"keychain\": 5.0\n", - " }\n", " total_price = 0.0\n", + "\n", " for product, quantity in customer_orders.items():\n", - " total_price += prices[product] * quantity\n", + " valid_input = False\n", + "\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + "\n", + " if price >= 0:\n", + " total_price += price * quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric price.\")\n", + "\n", " return total_price" ] }, @@ -154,13 +215,14 @@ "# Cell 5: update_inventory function\n", "def update_inventory(customer_orders, inventory):\n", " for product, quantity in customer_orders.items():\n", - " inventory[product] += quantity\n", + " inventory[product] -= quantity\n", + "\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "1f005003", "metadata": {}, "outputs": [ @@ -168,9 +230,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "Customer orders: {'t-shirt': 7, 'mug': 7, 'hat': 7, 'book': 7, 'keychain': 7}\n", - "Total price: 525.0\n", - "Updated inventory: {'t-shirt': 7, 'mug': 7, 'hat': 7, 'book': 7, 'keychain': 7}\n" + "Error: That product is out of stock.\n", + "Error: That product is out of stock.\n", + "Error: That product is not in the inventory.\n", + "Error: That product is out of stock.\n", + "Error: That product is not in the inventory.\n" ] } ],