diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..9af4d78 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,306 @@ "\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": 14, + "id": "c74805e9-b8f8-4485-a828-c324abd17294", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs in the inventory: 23\n", + "Enter the quantity of hats in the inventory: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Value error! Enter a non-negative number: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats in the inventory: ze\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'ze'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats in the inventory: 15\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 23, 'hat': 15}\n" + ] + } + ], + "source": [ + "# Step 1: Define the function for initializing the inventory with error handling\n", + "\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 in the inventory:\"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Value error! Enter a non-negative number: \")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " \n", + " inventory[product] = quantity\n", + "\n", + " return inventory\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "859a28b3-2ed6-463f-9725-bd81ee68d4b2", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Input is invalid. Please enter a non negative value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: 23\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Input is invalid. Please enter a non negative value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: 14\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Input is invalid. Please enter a non negative value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 44\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Input is invalid. Please enter a non negative value\n" + ] + }, + { + "data": { + "text/plain": [ + "['book', 'hat', 'mug']" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 2. Modify the `calculate_total_price` function to include error handling.\n", + "def calculate_total_price(customer_orders):\n", + " products_price = []\n", + " \n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " prod_price = int(input(f\"Enter the price of {product}: \"))\n", + " if prod_price >= 0:\n", + " products_price.append(product)\n", + " valid_input = True\n", + " raise ValueError(\"Input is invalid. Please enter a non negative value\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return products_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "7fb89bc1-a4b3-4649-bb36-f76267ecdf6e", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of orders: 3\n", + "Enter the product name: HOT\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Error: either you entered a wrong product or the product is unavailable!, try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: HAT\n", + "Enter the product name: BOOK\n", + "Enter the product name: 123\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Error: either you entered a wrong product or the product is unavailable!, try again\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: MUG\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "({'book', 'hat', 'mug'}, 3)\n" + ] + } + ], + "source": [ + "# 3. Modify the `get_customer_orders` function to include error handling\n", + "def get_customer_orders(inventory: dict):\n", + " \"\"\"\n", + " collects:\n", + " 1. Number of orders and \n", + " 2. Product names\n", + "\n", + " input: inventory -> dict\n", + "\n", + " output: number of orders, set of products (tuple)\n", + " \n", + " \"\"\"\n", + " \n", + " # 1. Collect Number of orders\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of orders: \"))\n", + " if number_of_orders < 0 :\n", + " raise ValueError(f\"Number {number_of_orders} is negative. Enter a non-negative number: \")\n", + " valid_input = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " \n", + " #2. Collect products\n", + " customer_orders = set()\n", + " while len(customer_orders) < number_of_orders:\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = (input(\"Enter the product name:\")).lower()\n", + " #Compare product to inventory & check nbr in inventory:\n", + " if product in inventory.keys() and inventory[product] > 0:\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " else:\n", + " raise ValueError(\"Error: either you entered a wrong product or the product is unavailable!, try again\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " \n", + " return customer_orders ,number_of_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "9ecc97fc-6099-463e-a940-9875aa41a817", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of orders: 4\n", + "Enter the product name: HAT\n", + "Enter the product name: MUG\n", + "Enter the product name: BOOK\n", + "Enter the product name: SKIRT\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "({'skirt', 'mug', 'hat', 'book'}, 4)\n" + ] + } + ], + "source": [ + "# 4. Test your code by running the program and deliberately entering\n", + "# invalid quantities and product names. Make sure the error handling mechanism works as expected.\n", + "products = [\"hat\", \"mug\", \"book\", \"t-shirt\", \"skirt\"]\n", + "#print(initialize_inventory(products))\n", + "print(get_customer_orders({'hat': 10, 'mug': 10, 'book': 10, 't-shirt': 10, 'skirt': 10}))\n" + ] } ], "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 +383,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,