From c89d9f9c1c04211431724570da06163224f66898 Mon Sep 17 00:00:00 2001 From: kraussk9 Date: Fri, 19 Jun 2026 15:25:41 +0100 Subject: [PATCH] Solved lab --- ...lab-python-error-handling-checkpoint.ipynb | 425 ++++++++++++++++++ lab-python-error-handling.ipynb | 333 +++++++++++++- 2 files changed, 755 insertions(+), 3 deletions(-) create mode 100644 .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb new file mode 100644 index 0000000..f393008 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,425 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Error Handling" + ] + }, + { + "cell_type": "markdown", + "id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b", + "metadata": {}, + "source": [ + "## Exercise: Error Handling for Managing Customer Orders\n", + "\n", + "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", + "\n", + "```python\n", + "# 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\n", + "```\n", + "\n", + "Let's enhance your code by implementing error handling to handle invalid inputs.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "2. Modify the `calculate_total_price` function to include error handling.\n", + " - If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n", + "\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), 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, display an error message and ask them to re-enter the product name. *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", + "\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": "086524fa-9879-4578-b51e-50b78ec9d981", + "metadata": {}, + "outputs": [], + "source": [ + "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": 10, + "id": "fd2cac2b-7d70-4369-973b-69fdc9b76c60", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: -8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity cannot be negative. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: t\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: 6\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 4\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 6, 'mug': 5, 'hat': 4, 'book': 3, 'keychain': 2}\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "532a54ce-1489-4e14-af09-38d2b333d61f", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " prices = {}\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price >= 0:\n", + " prices[product] = price\n", + " valid_price = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid price.\")\n", + " total_price = sum(prices.values())\n", + " return total_price\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "05a7dd26-6430-4a66-bba6-bf3aaae6787b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of tshirt: 22.50\n", + "Enter the price of mug: f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid price.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: -9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Price cannot be negative. Please enter a valid price.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 10\n", + "Enter the price of hat: 6\n", + "Enter the price of book: 5\n", + "Enter the price of keychain: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 45.5\n" + ] + } + ], + "source": [ + "customer_orders = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "723b22a9-8c05-41f4-a951-ec9b2fee364c", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_num = False\n", + " while not valid_num:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders >= 0:\n", + " valid_num = True\n", + " else:\n", + " print(\"Number of orders cannot be negative. Please enter a valid number.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product = input(\"Enter the name of the product that a customer wants to order: \")\n", + " if product in inventory:\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " else:\n", + " print(\"Product not found in inventory. Please enter a valid product name.\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "07e513b0-ec33-4709-99e1-77c27342f839", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: -9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity cannot be negative. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: 6\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 8\n", + "Enter the quantity of books available: 9\n", + "Enter the quantity of keychains available: 3\n", + "Enter the number of customer orders: h\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: -7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of orders cannot be negative. Please enter a valid number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of the product that a customer wants to order: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product not found in inventory. Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product that a customer wants to order: -3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product not found in inventory. Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product that a customer wants to order: hat\n", + "Enter the name of the product that a customer wants to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'hat'}\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf57c9f7-4015-4f5d-a969-61dbf4bc7cd5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..f393008 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,340 @@ "\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": "086524fa-9879-4578-b51e-50b78ec9d981", + "metadata": {}, + "outputs": [], + "source": [ + "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": 10, + "id": "fd2cac2b-7d70-4369-973b-69fdc9b76c60", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: -8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity cannot be negative. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: t\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: 6\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 4\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 6, 'mug': 5, 'hat': 4, 'book': 3, 'keychain': 2}\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "532a54ce-1489-4e14-af09-38d2b333d61f", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " prices = {}\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price >= 0:\n", + " prices[product] = price\n", + " valid_price = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid price.\")\n", + " total_price = sum(prices.values())\n", + " return total_price\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "05a7dd26-6430-4a66-bba6-bf3aaae6787b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of tshirt: 22.50\n", + "Enter the price of mug: f\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid price.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: -9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Price cannot be negative. Please enter a valid price.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 10\n", + "Enter the price of hat: 6\n", + "Enter the price of book: 5\n", + "Enter the price of keychain: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 45.5\n" + ] + } + ], + "source": [ + "customer_orders = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "723b22a9-8c05-41f4-a951-ec9b2fee364c", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_num = False\n", + " while not valid_num:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders >= 0:\n", + " valid_num = True\n", + " else:\n", + " print(\"Number of orders cannot be negative. Please enter a valid number.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product = input(\"Enter the name of the product that a customer wants to order: \")\n", + " if product in inventory:\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " else:\n", + " print(\"Product not found in inventory. Please enter a valid product name.\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "07e513b0-ec33-4709-99e1-77c27342f839", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: -9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity cannot be negative. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of tshirts available: 6\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 8\n", + "Enter the quantity of books available: 9\n", + "Enter the quantity of keychains available: 3\n", + "Enter the number of customer orders: h\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: -7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of orders cannot be negative. Please enter a valid number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of the product that a customer wants to order: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product not found in inventory. Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product that a customer wants to order: -3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product not found in inventory. Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the product that a customer wants to order: hat\n", + "Enter the name of the product that a customer wants to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'hat'}\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf57c9f7-4015-4f5d-a969-61dbf4bc7cd5", + "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 +417,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,