From 25f470ffdd85f02a5802e4f3dfdb909adc62dcdf Mon Sep 17 00:00:00 2001 From: baharmnb Date: Thu, 26 Mar 2026 15:13:38 +0100 Subject: [PATCH] solved lab --- ...lab-python-error-handling-checkpoint.ipynb | 233 ++++++++++++++++++ lab-python-error-handling.ipynb | 137 +++++++++- 2 files changed, 369 insertions(+), 1 deletion(-) 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..263c7d3 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,233 @@ +{ + "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": 21, + "id": "d94350e1-80a5-412f-830c-80298e182aff", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products):\n", + " total_price=0\n", + " for product in products:\n", + " \n", + " while True:\n", + " try:\n", + " price = int(input(f\"Enter the price of {product} : \")) \n", + " \n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative. Please enter a valid price.\")\n", + " continue\n", + " else: \n", + " total_price += price\n", + " break\n", + " \n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + "\n", + " \n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "08c4b1c2-a651-440d-b10f-7e258e1341f1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " \n", + " for product in products:\n", + " \n", + " while True:\n", + " try:\n", + " order_number = int(input(\"How many different products do you want to order? \")) \n", + " \n", + " if order_number < 0:\n", + " print(\"Error: order number cannot be negative. Please enter a valid number.\")\n", + " continue\n", + " \n", + " break\n", + " \n", + " except ValueError:\n", + " print(\"Error: Please enter a valid whole number.\")\n", + " customer_orders = []\n", + "\n", + " for i in range(order_number):\n", + " while True:\n", + " product_name = input(f\"Enter product name {i + 1}: \").strip()\n", + "\n", + " if product_name not in inventory:\n", + " print(\"Error: That product is not in the inventory. Please try again.\")\n", + " continue\n", + "\n", + " if inventory[product_name] <= 0:\n", + " print(\"Error: That product is out of stock. Please choose another product.\")\n", + " continue\n", + "\n", + " customer_orders.append(product_name)\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "57f9dbe3-5bdd-4bb1-9dc2-d7b80017e09e", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat : 7\n", + "Enter the price of boot : 8\n", + "Enter the price of short : 9\n" + ] + }, + { + "data": { + "text/plain": [ + "24" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products=['hat','boot','short']\n", + "\n", + "calculate_total_price(products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93ba620c-efc4-4ecf-9517-321b6cd3c058", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many different products do you want to order? 7\n", + "How many different products do you want to order? 8\n" + ] + } + ], + "source": [ + "inventory=['hat','boot','short','t-shirt','jean']\n", + "get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc6e7d0f-b303-44ee-a7d8-0759cdb07ddc", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..263c7d3 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,141 @@ "\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": 21, + "id": "d94350e1-80a5-412f-830c-80298e182aff", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products):\n", + " total_price=0\n", + " for product in products:\n", + " \n", + " while True:\n", + " try:\n", + " price = int(input(f\"Enter the price of {product} : \")) \n", + " \n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative. Please enter a valid price.\")\n", + " continue\n", + " else: \n", + " total_price += price\n", + " break\n", + " \n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + "\n", + " \n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "08c4b1c2-a651-440d-b10f-7e258e1341f1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " \n", + " for product in products:\n", + " \n", + " while True:\n", + " try:\n", + " order_number = int(input(\"How many different products do you want to order? \")) \n", + " \n", + " if order_number < 0:\n", + " print(\"Error: order number cannot be negative. Please enter a valid number.\")\n", + " continue\n", + " \n", + " break\n", + " \n", + " except ValueError:\n", + " print(\"Error: Please enter a valid whole number.\")\n", + " customer_orders = []\n", + "\n", + " for i in range(order_number):\n", + " while True:\n", + " product_name = input(f\"Enter product name {i + 1}: \").strip()\n", + "\n", + " if product_name not in inventory:\n", + " print(\"Error: That product is not in the inventory. Please try again.\")\n", + " continue\n", + "\n", + " if inventory[product_name] <= 0:\n", + " print(\"Error: That product is out of stock. Please choose another product.\")\n", + " continue\n", + "\n", + " customer_orders.append(product_name)\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "57f9dbe3-5bdd-4bb1-9dc2-d7b80017e09e", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat : 7\n", + "Enter the price of boot : 8\n", + "Enter the price of short : 9\n" + ] + }, + { + "data": { + "text/plain": [ + "24" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products=['hat','boot','short']\n", + "\n", + "calculate_total_price(products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93ba620c-efc4-4ecf-9517-321b6cd3c058", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many different products do you want to order? 7\n", + "How many different products do you want to order? 8\n" + ] + } + ], + "source": [ + "inventory=['hat','boot','short','t-shirt','jean']\n", + "get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc6e7d0f-b303-44ee-a7d8-0759cdb07ddc", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +225,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,