diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..f12cfeb 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -23,6 +23,13 @@ "\n", "```python\n", "# Step 1: Define the function for initializing the inventory with error handling\n", + "\n", + "#while not valid_quantity: loop will repeatedly ask for the user input until valid_quantity is set to True. Essentially, this creates a loop that continues until the user provides a valid input.\n", + "\n", + "#valid_quantity is set to False. This acts as a flag to indicate that a valid quantity has not yet been entered.\n", + "\n", + "# if both checks pass without exceptions being raised, valid_quantity is set to True, which breaks the loop.\n", + "\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", @@ -72,13 +79,204 @@ "\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": "ad020a96-5381-41f7-a0db-05b3b2f6a7c9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price per each t-shirt in customer orders 3\n", + "Enter the price per each mug in customer orders 4\n", + "Enter the price per each hat in customer orders 5\n", + "Enter the price per each book in customer orders 6\n", + "Enter the price per each keychain in customer orders 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " tot_price_customer_orders is: 25.0\n" + ] + } + ], + "source": [ + "#2\n", + "\n", + "customer_order = [\"bike\", \"hat\", \"mug\"]\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def tot_price_customer_orders():\n", + " total_price = 0 # Don't forget to initialize total_price\n", + " for product in products: \n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " \n", + " product_price = float(input(f\"Enter the price per each {product} in customer orders\"))\n", + " \n", + " if product_price < 0: # Ensure consistent indentation for if statement\n", + " raise ValueError(\"Invalid price. Insert the price per product again\")\n", + " \n", + " total_price += product_price\n", + " valid_input = True\n", + " \n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return total_price # Consistently indented with def line\n", + "\n", + "total_price = tot_price_customer_orders () # Call the function and store the result\n", + "\n", + "print (\" tot_price_customer_orders is:\", total_price)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7bed5739-83b1-4ef2-beb3-84470ce70f39", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter product name: hat\n", + "Enter product name: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "customer_orders are: {'hat', 'book'}\n" + ] + } + ], + "source": [ + "#3\n", + "def get_customer_orders(n_of_orders):\n", + " customer_orders = set() # Initialize a set to collect unique product names\n", + " for _ in range(n_of_orders):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " product_name = input(\"Enter product name: \")\n", + " if not product_name.strip():\n", + " raise ValueError(\"Product name cannot be empty.\")\n", + " customer_orders.add(product_name) # Add to the set\n", + " valid_input = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return customer_orders\n", + "\n", + "try:\n", + " n_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if n_of_orders <= 0:\n", + " raise ValueError(\"The number of customer orders should be a positive integer.\")\n", + " \n", + " customer_orders = get_customer_orders(n_of_orders)\n", + " print(\"customer_orders are:\", customer_orders)\n", + "except ValueError as error:\n", + " print(f\"Error: {error}. Please restart the program and enter a valid number.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "81d9eff6-cd54-49ce-96a6-4e7739c79b10", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter product name that is available table\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product name is not available .\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product name that is available book\n", + "Enter product name that is available mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "customer_orders are: {'book', 'mug'}\n" + ] + } + ], + "source": [ + "#4\n", + "\n", + "inventory = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def get_customer_orders(n_of_orders, inventory):\n", + " customer_orders = set() # Initialize a set to collect unique product names\n", + " for _ in range(n_of_orders):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " product_name = input(\"Enter product name that is available\")\n", + " if product_name not in inventory:\n", + " raise ValueError(\"Product name is not available .\")\n", + " customer_orders.add(product_name) # Add to the set\n", + " valid_input = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return customer_orders\n", + "\n", + "try:\n", + " n_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if n_of_orders <= 0:\n", + " raise ValueError(\"The number of customer orders should be a positive integer.\")\n", + " \n", + " customer_orders = get_customer_orders(n_of_orders, inventory)\n", + " print(\"customer_orders are:\", customer_orders)\n", + "except ValueError as error:\n", + " print(f\"Error: {error}. Please restart the program and enter a valid number.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f2d1d9ee-1233-4f51-aa74-a6c714d7384b", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c92a3e0c-16c1-4dce-ba96-f0febc5219c8", + "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 +288,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,