From 0cf7b1a3a15f7318ac24a54ea0d78cbe273cde63 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Mon, 3 Nov 2025 23:48:39 +0100 Subject: [PATCH 1/7] commit --- lab-python-error-handling.ipynb | 54 +++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..63a89d2 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,61 @@ "\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": "4f673274", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the function for initializing the inventory with error handling\n", + "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", + "# Or, in another way:\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:\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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "83d62dfe", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +140,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From 58065c52624c537d679236ad278d6491b7e89d16 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Sun, 28 Dec 2025 06:00:45 +0100 Subject: [PATCH 2/7] commit --- lab-python-error-handling.ipynb | 205 +++++++++++++++++++++++++++++++- testing.ipynb | 76 ++++++++++++ 2 files changed, 275 insertions(+), 6 deletions(-) create mode 100644 testing.ipynb diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 63a89d2..82fc7ed 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -2,10 +2,11 @@ "cells": [ { "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "id": "40485476", "metadata": {}, "source": [ - "# Lab | Error Handling" + "# Lab | Error Handling\n", + "---" ] }, { @@ -18,8 +19,8 @@ "The implementation of your code for managing customer orders assumes that the user will always enter a valid input. \n", "\n", "For example, we could modify the `initialize_inventory` function to include error handling.\n", - " - If the user enters an invalid quantity (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the quantity for that product.\n", - " - Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n", + " - **If the user enters an invalid quantity** (e.g., a negative value or a non-numeric value), **display an error message** and **ask them to re-enter** the quantity for that product.\n", + " - Use a **``try-except``** block to handle the error and continue prompting the user **until a valid quantity is entered**.\n", "\n", "```python\n", "# Step 1: Define the function for initializing the inventory with error handling\n", @@ -73,6 +74,27 @@ "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": "markdown", + "id": "549c919f", + "metadata": {}, + "source": [ + "---\n", + "# Solved Lab | Error Handling" + ] + }, + { + "cell_type": "markdown", + "id": "399e7e87", + "metadata": {}, + "source": [ + "---\n", + "### STEP 1\n", + "modify the `initialize_inventory` function to include error handling.\n", + " - **If the user enters an invalid quantity** (e.g., a negative value or a non-numeric value), **display an error message** and **ask them to re-enter** the quantity for that product.\n", + " - Use a **``try-except``** block to handle the error and continue prompting the user **until a valid quantity is entered**." + ] + }, { "cell_type": "code", "execution_count": 1, @@ -115,13 +137,184 @@ " return inventory" ] }, + { + "cell_type": "markdown", + "id": "7711e9ff", + "metadata": {}, + "source": [ + "---\n", + "### STEP 2\n", + "2. Modify the `calculate_total_price` function to include error handling.\n", + " \n", + " - If the user enters an invalid price (e.g., a negative value or a non-numeric value),\n", + " - display an error message and ask them to re-enter the price for that product.\n", + " \n", + " - Use a try-except block to handle the error and continue prompting the user until a valid price is entered." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1136ebce", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products):\n", + " \"\"\"Calculate total price with error handling for invalid price inputs\"\"\"\n", + " total_price = 0.0\n", + " \n", + " for product in products:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price for {product}: $\"))\n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative. Please enter a valid price.\")\n", + " continue\n", + " total_price += price\n", + " break # Valid price entered, move to next product\n", + " except ValueError:\n", + " print(\"Error: Invalid price. Please enter a numeric value.\")\n", + " \n", + " return total_price" + ] + }, + { + "cell_type": "markdown", + "id": "63b63ca9", + "metadata": {}, + "source": [ + "---\n", + "### STEP 3\n", + "3. Modify the `get_customer_orders` function to include error handling.\n", + " - If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), \n", + " - display an error message and ask them to re-enter the number of orders.\n", + " - If the user enters an invalid product name (e.g., a product name that is not in the inventory), \n", + " - or that doesn't have stock available, display an error message and ask them to re-enter the product name. \n", + " - *Hint: you will need to pass inventory as a parameter*\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "83d62dfe", + "id": "ae63c5c8", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def get_customer_orders(inventory):\n", + " \"\"\"Get customer orders with full error handling for quantity and product validation\"\"\"\n", + " customer_orders = []\n", + " \n", + " # Validate number of orders\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of orders: \"))\n", + " if num_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative. Please try again.\")\n", + " continue\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Invalid number. Please enter a whole number.\")\n", + " \n", + " # Get each order with product validation\n", + " for _ in range(num_orders):\n", + " while True:\n", + " product = input(\"Enter the product name: \").strip()\n", + " if product not in inventory:\n", + " print(f\"Error: '{product}' not in inventory. Available: {list(inventory.keys())}\")\n", + " continue\n", + " if inventory[product] <= 0:\n", + " print(f\"Error: '{product}' is out of stock.\")\n", + " continue\n", + " customer_orders.append(product)\n", + " break\n", + " \n", + " return customer_orders\n" + ] + }, + { + "cell_type": "markdown", + "id": "f55ac6e6", + "metadata": {}, + "source": [ + "---\n", + "### STEP 4\n", + "4. Test your code by running the program and deliberately entering invalid quantities and product names. \n", + "- Make sure the error handling mechanism works as expected." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e342261e", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " \"\"\"Initialize inventory with error handling for quantities\"\"\"\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " if quantity < 0:\n", + " print(\"Error: Quantity cannot be negative.\")\n", + " continue\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Enter a whole number.\")\n", + " return inventory\n", + "\n", + "def get_customer_orders(inventory):\n", + " customer_orders = []\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter number of orders: \"))\n", + " if num_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative.\")\n", + " continue\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Enter a whole number.\")\n", + " \n", + " for _ in range(num_orders):\n", + " while True:\n", + " product = input(\"Enter product name: \").strip()\n", + " if product not in inventory:\n", + " print(f\"Error: '{product}' not in inventory. Available: {list(inventory.keys())}\")\n", + " continue\n", + " if inventory[product] <= 0:\n", + " print(f\"Error: '{product}' out of stock.\")\n", + " continue\n", + " customer_orders.append(product)\n", + " break\n", + " return customer_orders\n", + "\n", + "def calculate_total_price(products):\n", + " total_price = 0.0\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter price for {product}: $\"))\n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative.\")\n", + " continue\n", + " total_price += price\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Enter numeric value.\")\n", + " return total_price\n", + "\n", + "# Main program\n", + "if __name__ == \"__main__\":\n", + " products = [\"Chocolate Bar\", \"Truffle Box\", \"Candy Cane\"]\n", + " inventory = initialize_inventory(products)\n", + " orders = get_customer_orders(inventory)\n", + " total = calculate_total_price(orders)\n", + " print(f\"\\nFinal order: {orders}\")\n", + " print(f\"Total price: ${total:.2f}\")[conversation_history:6]\n" + ] } ], "metadata": { diff --git a/testing.ipynb b/testing.ipynb new file mode 100644 index 0000000..f134127 --- /dev/null +++ b/testing.ipynb @@ -0,0 +1,76 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "8b610ed5", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity of {product}s: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " break\n", + " print(\"Quantity cannot be negative!\")\n", + " except ValueError:\n", + " print(\"Enter a valid number!\")\n", + " return inventory\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter price for {product}: $\"))\n", + " if price >= 0:\n", + " total_price += price * customer_orders[product]\n", + " break\n", + " print(\"Price cannot be negative!\")\n", + " except ValueError:\n", + " print(\"Enter a valid number!\")\n", + " return total_price\n", + "\n", + "def get_customer_orders(inventory):\n", + " customer_orders = {}\n", + " \n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"How many different products? \"))\n", + " if num_orders > 0:\n", + " break\n", + " print(\"Number must be positive!\")\n", + " except ValueError:\n", + " print(\"Enter a valid number!\")\n", + " \n", + " for _ in range(num_orders):\n", + " while True:\n", + " product = input(\"What product? \").strip()\n", + " if product in inventory and inventory[product] > 0:\n", + " customer_orders[product] = 1\n", + " break\n", + " print(f\"{product} not available!\")\n", + " \n", + " return customer_orders\n", + "\n", + "# Test it\n", + "products = [\"t-shirt\", \"mug\", \"hat\"]\n", + "inventory = initialize_inventory(products)\n", + "orders = get_customer_orders(inventory)\n", + "total = calculate_total_price(orders)\n", + "print(f\"Total: ${total}\")\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From e6bddf1a0d2e1485cf09397e6c5b840fa91159ad Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Wed, 31 Dec 2025 05:05:14 +0100 Subject: [PATCH 3/7] commit --- lab-python-error-handling.ipynb | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 82fc7ed..4fa5c37 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -248,7 +248,26 @@ "execution_count": null, "id": "e342261e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Enter a whole number.\n", + "Error: Enter a whole number.\n", + "Error: 'kjlhkjh' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: 'chocolate bar' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: 'book' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: 'Chocolate bar' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: 'aa' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: 'aa' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: 'a' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: 'a' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: '15' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: '1564+' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n" + ] + } + ], "source": [ "def initialize_inventory(products):\n", " \"\"\"Initialize inventory with error handling for quantities\"\"\"\n", @@ -315,6 +334,16 @@ " print(f\"\\nFinal order: {orders}\")\n", " print(f\"Total price: ${total:.2f}\")[conversation_history:6]\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4f4a0881", + "metadata": {}, + "outputs": [], + "source": [ + "3" + ] } ], "metadata": { From 758f4a324f009e24934cf2c9b0d06ac589336d07 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Wed, 7 Jan 2026 05:59:01 +0100 Subject: [PATCH 4/7] update notebook --- lab-python-error-handling.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 4fa5c37..53f5865 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -187,11 +187,11 @@ "### STEP 3\n", "3. Modify the `get_customer_orders` function to include error handling.\n", " - If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), \n", - " - display an error message and ask them to re-enter the number of orders.\n", - " - If the user enters an invalid product name (e.g., a product name that is not in the inventory), \n", - " - or that doesn't have stock available, display an error message and ask them to re-enter the product name. \n", - " - *Hint: you will need to pass inventory as a parameter*\n", - " - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n" + " - display an error message and ask them to re-enter the number of orders.\n", + " - If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, \n", + " - display an error message and ask them to re-enter the product name. \n", + " - *Hint*: you will need to pass inventory as a parameter*\n", + " - Use a `try-except` block to handle the error and continue prompting the user until a valid product name is entered.\n" ] }, { From 912ffeed65497909736f348ebf42a8f521ad6a16 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Wed, 7 Jan 2026 06:05:53 +0100 Subject: [PATCH 5/7] update notebook --- lab-python-error-handling.ipynb | 224 ++++++++++++++++++++++---------- testing.ipynb | 76 ----------- 2 files changed, 158 insertions(+), 142 deletions(-) delete mode 100644 testing.ipynb diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 53f5865..579b610 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -79,6 +79,7 @@ "id": "549c919f", "metadata": {}, "source": [ + "---\n", "---\n", "# Solved Lab | Error Handling" ] @@ -97,44 +98,68 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "4f673274", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input: invalid literal for int() with base 10: 'a'. Please enter a non-negative integer.\n", + "Invalid input: invalid literal for int() with base 10: 'f'. Please enter a non-negative integer.\n", + "Invalid input: invalid literal for int() with base 10: 'g'. Please enter a non-negative integer.\n", + "Invalid input: invalid literal for int() with base 10: 'apple'. Please enter a non-negative integer.\n", + "Invalid input: invalid literal for int() with base 10: 'Apple'. Please enter a non-negative integer.\n", + "Invalid input: invalid literal for int() with base 10: 'Apples'. Please enter a non-negative integer.\n", + "Invalid input: invalid literal for int() with base 10: 'Cake'. Please enter a non-negative integer.\n", + "Invalid input: invalid literal for int() with base 10: ''. Please enter a non-negative integer.\n", + "Invalid input: invalid literal for int() with base 10: ''. Please enter a non-negative integer.\n" + ] + } + ], "source": [ "# Step 1: Define the function for initializing the inventory with error handling\n", "def initialize_inventory(products):\n", + " \"\"\"\n", + " Initialize inventory quantities for a list of products.\n", + " Ensures that the user enters valid non-negative integers.\n", + " \n", + " Args:\n", + " products (list): List of product names.\n", + " \n", + " Returns:\n", + " dict: Dictionary with product names as keys and quantities as values.\n", + " \"\"\"\n", " inventory = {}\n", + " \n", " for product in products:\n", - " valid_quantity = False\n", - " while not valid_quantity:\n", + " while True:\n", " try:\n", - " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " # Ask the user for quantity\n", + " quantity = input(f\"Enter quantity for {product}: \")\n", + " \n", + " # Convert input to integer\n", + " quantity = int(quantity)\n", + " \n", + " # Check for negative values\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", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " \n", + " # Valid quantity, store it and break the loop\n", + " inventory[product] = quantity\n", + " break\n", + " \n", + " except ValueError as e:\n", + " # Handle non-numeric or negative inputs\n", + " print(f\"Invalid input: {e}. Please enter a non-negative integer.\")\n", + " \n", " return inventory\n", "\n", - "# Or, in another way:\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:\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" + "# Test the function:\n", + "products_list = [\"Apples\", \"Bananas\", \"Oranges\"]\n", + "inventory = initialize_inventory(products_list)\n", + "print(\"Inventory initialized:\", inventory)\n" ] }, { @@ -154,28 +179,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "1136ebce", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input: could not convert string to float: 'a'. Please enter a non-negative number.\n", + "Total price: $45.00\n" + ] + } + ], "source": [ "def calculate_total_price(products):\n", - " \"\"\"Calculate total price with error handling for invalid price inputs\"\"\"\n", + " \"\"\"\n", + " Ask the user to enter the price for each product and calculate the total price.\n", + " Ensures valid, non-negative numeric input.\n", + "\n", + " Args:\n", + " products (list): List of product names.\n", + "\n", + " Returns:\n", + " float: Total price of all products.\n", + " \"\"\"\n", " total_price = 0.0\n", " \n", " for product in products:\n", " while True:\n", " try:\n", - " price = float(input(f\"Enter the price for {product}: $\"))\n", + " # Ask user for the price\n", + " price = input(f\"Enter price for {product}: \")\n", + " \n", + " # Convert input to float\n", + " price = float(price)\n", + " \n", + " # Check for negative price\n", " if price < 0:\n", - " print(\"Error: Price cannot be negative. Please enter a valid price.\")\n", - " continue\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " \n", + " # Valid price, add to total and break loop\n", " total_price += price\n", - " break # Valid price entered, move to next product\n", - " except ValueError:\n", - " print(\"Error: Invalid price. Please enter a numeric value.\")\n", + " break\n", + " \n", + " except ValueError as e:\n", + " print(f\"Invalid input: {e}. Please enter a non-negative number.\")\n", " \n", - " return total_price" + " return total_price\n", + "\n", + "# Example usage:\n", + "products_list = [\"Apples\", \"Bananas\", \"Oranges\"]\n", + "total = calculate_total_price(products_list)\n", + "print(f\"Total price: ${total:.2f}\")\n" ] }, { @@ -202,34 +258,72 @@ "outputs": [], "source": [ "def get_customer_orders(inventory):\n", - " \"\"\"Get customer orders with full error handling for quantity and product validation\"\"\"\n", - " customer_orders = []\n", - " \n", - " # Validate number of orders\n", + " \"\"\"\n", + " Ask the user for their orders, including quantity and product name.\n", + " Validates:\n", + " - Number of orders (non-negative integer)\n", + " - Product exists in inventory\n", + " - Product has available stock\n", + "\n", + " Args:\n", + " inventory (dict): Dictionary of product names and available quantities\n", + "\n", + " Returns:\n", + " dict: Customer orders with product names and ordered quantities\n", + " \"\"\"\n", + " orders = {}\n", + "\n", + " print(\"\\nEnter your orders:\")\n", + "\n", " while True:\n", " try:\n", - " num_orders = int(input(\"Enter the number of orders: \"))\n", + " num_orders = input(\"How many different products do you want to order? \")\n", + " num_orders = int(num_orders)\n", " if num_orders < 0:\n", - " print(\"Error: Number of orders cannot be negative. Please try again.\")\n", - " continue\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", " break\n", - " except ValueError:\n", - " print(\"Error: Invalid number. Please enter a whole number.\")\n", - " \n", - " # Get each order with product validation\n", - " for _ in range(num_orders):\n", + " except ValueError as e:\n", + " print(f\"Invalid input: {e}. Please enter a non-negative integer.\")\n", + "\n", + " for i in range(num_orders):\n", " while True:\n", - " product = input(\"Enter the product name: \").strip()\n", - " if product not in inventory:\n", - " print(f\"Error: '{product}' not in inventory. Available: {list(inventory.keys())}\")\n", + " product_name = input(f\"Enter product name for order #{i+1}: \").strip()\n", + " \n", + " # Check if product exists in inventory\n", + " if product_name not in inventory:\n", + " print(\"Invalid product name. Please choose a product from the inventory.\")\n", " continue\n", - " if inventory[product] <= 0:\n", - " print(f\"Error: '{product}' is out of stock.\")\n", + " \n", + " # Check if product has stock available\n", + " if inventory[product_name] == 0:\n", + " print(f\"Sorry, {product_name} is out of stock. Please choose another product.\")\n", " continue\n", - " customer_orders.append(product)\n", + " \n", + " # Ask for quantity\n", + " while True:\n", + " try:\n", + " quantity = input(f\"Enter quantity for {product_name}: \")\n", + " quantity = int(quantity)\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " if quantity > inventory[product_name]:\n", + " raise ValueError(f\"Only {inventory[product_name]} units of {product_name} available.\")\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Invalid input: {e}. Please try again.\")\n", + " \n", + " # Save order and reduce inventory\n", + " orders[product_name] = quantity\n", + " inventory[product_name] -= quantity\n", " break\n", - " \n", - " return customer_orders\n" + "\n", + " return orders\n", + "\n", + "# Example usage\n", + "inventory = {\"Apples\": 10, \"Bananas\": 5, \"Oranges\": 8}\n", + "customer_orders = get_customer_orders(inventory)\n", + "print(\"\\nCustomer Orders:\", customer_orders)\n", + "print(\"Updated Inventory:\", inventory)\n" ] }, { @@ -255,16 +349,14 @@ "text": [ "Error: Enter a whole number.\n", "Error: Enter a whole number.\n", - "Error: 'kjlhkjh' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: Enter a whole number.\n", + "Error: Enter a whole number.\n", + "Error: Enter a whole number.\n", + "Error: Enter a whole number.\n", + "Error: 'ddd' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", "Error: 'chocolate bar' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", - "Error: 'book' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", - "Error: 'Chocolate bar' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", - "Error: 'aa' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", - "Error: 'aa' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", - "Error: 'a' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", - "Error: 'a' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", - "Error: '15' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", - "Error: '1564+' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n" + "Error: 'hehe' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n", + "Error: 'aa' not in inventory. Available: ['Chocolate Bar', 'Truffle Box', 'Candy Cane']\n" ] } ], diff --git a/testing.ipynb b/testing.ipynb deleted file mode 100644 index f134127..0000000 --- a/testing.ipynb +++ /dev/null @@ -1,76 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "8b610ed5", - "metadata": {}, - "outputs": [], - "source": [ - "def initialize_inventory(products):\n", - " inventory = {}\n", - " for product in products:\n", - " while True:\n", - " try:\n", - " quantity = int(input(f\"Enter quantity of {product}s: \"))\n", - " if quantity >= 0:\n", - " inventory[product] = quantity\n", - " break\n", - " print(\"Quantity cannot be negative!\")\n", - " except ValueError:\n", - " print(\"Enter a valid number!\")\n", - " return inventory\n", - "\n", - "def calculate_total_price(customer_orders):\n", - " total_price = 0\n", - " for product in customer_orders:\n", - " while True:\n", - " try:\n", - " price = float(input(f\"Enter price for {product}: $\"))\n", - " if price >= 0:\n", - " total_price += price * customer_orders[product]\n", - " break\n", - " print(\"Price cannot be negative!\")\n", - " except ValueError:\n", - " print(\"Enter a valid number!\")\n", - " return total_price\n", - "\n", - "def get_customer_orders(inventory):\n", - " customer_orders = {}\n", - " \n", - " while True:\n", - " try:\n", - " num_orders = int(input(\"How many different products? \"))\n", - " if num_orders > 0:\n", - " break\n", - " print(\"Number must be positive!\")\n", - " except ValueError:\n", - " print(\"Enter a valid number!\")\n", - " \n", - " for _ in range(num_orders):\n", - " while True:\n", - " product = input(\"What product? \").strip()\n", - " if product in inventory and inventory[product] > 0:\n", - " customer_orders[product] = 1\n", - " break\n", - " print(f\"{product} not available!\")\n", - " \n", - " return customer_orders\n", - "\n", - "# Test it\n", - "products = [\"t-shirt\", \"mug\", \"hat\"]\n", - "inventory = initialize_inventory(products)\n", - "orders = get_customer_orders(inventory)\n", - "total = calculate_total_price(orders)\n", - "print(f\"Total: ${total}\")\n" - ] - } - ], - "metadata": { - "language_info": { - "name": "python" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From 05b9f4451ba24d79f2b6b20ec68d70b6db289b26 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Sun, 1 Mar 2026 10:19:22 +0100 Subject: [PATCH 6/7] commit --- lab-python-error-handling.ipynb | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 579b610..bf93ae6 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -426,16 +426,6 @@ " print(f\"\\nFinal order: {orders}\")\n", " print(f\"Total price: ${total:.2f}\")[conversation_history:6]\n" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4f4a0881", - "metadata": {}, - "outputs": [], - "source": [ - "3" - ] } ], "metadata": { From b3e21e349d781461bfadbad00a33d23753569602 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Sun, 1 Mar 2026 10:20:05 +0100 Subject: [PATCH 7/7] commit --- lab-python-error-handling.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index bf93ae6..6627f0f 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -444,7 +444,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.13.5" + "version": "3.13.9" } }, "nbformat": 4,