diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..6ab42fd 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -59,24 +59,189 @@ "\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", + "Follow the steps below to complete the exercise:" + ] + }, + { + "cell_type": "markdown", + "id": "bb370b51", + "metadata": {}, + "source": [ "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", + " - 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": "e3d4a63b", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = ['hat', 'keychain']\n", "\n", + "def calculate_total_price():\n", + " valid_input = False \n", + " while not valid_input:\n", + " try:\n", + " prices = sum({order:int(input(f\"Enter a price for each {order} available\")) for order in customer_orders}.values())\n", + " if prices !=0:\n", + " sum_price = 0\n", + " valid_input = True\n", + " else: \n", + " print(\"Quantity not valid\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity\")\n", + " \n", + " return prices " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "d4287241", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input\n", + "Invalid input\n", + "Invalid input\n", + "Invalid input\n", + "Invalid input\n", + "Invalid input\n", + "Invalid input\n", + "Invalid input\n" + ] + }, + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_total_price()" + ] + }, + { + "cell_type": "markdown", + "id": "abd50211", + "metadata": {}, + "source": [ "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", + " - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "22cbee68", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "7ef88815", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", + "customer_orders = set()\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" + "def get_customers_orders(): \n", + " valid_input = False \n", + " while not valid_input:\n", + " try:\n", + " order_products = int(input(f\"Enter a number of products that you wish to order\")) \n", + " if order_products !=0:\n", + " valid_input = True\n", + " else: \n", + " print(\"Quantity not valid\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity\")\n", + " valid_input = False \n", + " while not valid_input:\n", + " for item in range(order_products):\n", + " try:\n", + " order = input(\"Enter a product\")\n", + " if order in inventory: \n", + " valid_input = True\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Product isn't available in inventory\")\n", + " except TypeError:\n", + " print(\"Add a valid product. Keep trying\")\n", + "\n", + " return customer_orders\n", + " " ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "714898d1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product isn't available in inventory\n", + "Product isn't available in inventory\n" + ] + }, + { + "data": { + "text/plain": [ + "{'hat', 'mug'}" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders = get_customers_orders()\n", + "customer_orders\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "b8a20f36", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "027c9e6e", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +255,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,