From 0020c4dbae8e49ab55016f2b943fc97f81fbfaf4 Mon Sep 17 00:00:00 2001 From: Francisco Date: Sun, 21 Jun 2026 19:27:23 +0200 Subject: [PATCH] error handling lab done --- ...lab-python-error-handling-checkpoint.ipynb | 592 ++++++++++++++++++ lab-python-error-handling.ipynb | 500 ++++++++++++++- 2 files changed, 1089 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..902a603 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,592 @@ +{ + "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": null, + "id": "fbd541e7-ca8c-4a92-a1a8-436b0753339a", + "metadata": {}, + "outputs": [], + "source": [ + "# 1 #" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "1ee3301b-aff2-4c18-a10a-858702770c31", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9b55cd8e-10de-4ac4-a8e5-e874838b7b0a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c826826d-cfdc-4073-84aa-6d0cbeb318ab", + "metadata": {}, + "outputs": [], + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3ba33d52-e055-4908-9b14-13762e5294b6", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 3\n", + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats available: 4\n", + "Enter the quantity of books available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of books available: 4\n", + "Enter the quantity of keychains available: 3\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9df818d0-17a8-45de-af33-4bd120334c1c", + "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": 6, + "id": "889b9468-f2d4-45a2-88f6-fda50c62a85b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 3\n", + "Enter the quantity of mugs available: 0\n", + "Enter the quantity of hats available: -1\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 hats available: d\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 hats available: 3\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 3\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "markdown", + "id": "db2d70e5-e48e-4221-af3b-66396f36cf11", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "e739efdb-18e8-4d30-b6d3-9dc5c5a73ec1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + "\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " customer_orders = {\n", + " input(\"Enter the name of a product that a customer wants to order: \")\n", + " for i in range(number_of_orders)\n", + " }\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "c751ac82-a58b-4796-8dff-9ebcbc4081fb", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 1\n", + "Enter the name of a product that a customer wants to order: hat\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "3a19a8bf-5668-4da6-94a8-47341846fc91", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'hat'}" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "bd4b9cf0-4e87-4d03-b26a-c942809bdadb", + "metadata": {}, + "outputs": [], + "source": [ + "def total_price(customer_orders):\n", + "\n", + " total = 0\n", + "\n", + " for product in customer_orders:\n", + "\n", + " price = int(input(f\"Enter the price of {product}: \"))\n", + " total += price\n", + "\n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "4212080c-6741-4e4c-b605-3394bd2bb221", + "metadata": {}, + "outputs": [], + "source": [ + "def total_price(customer_orders):\n", + "\n", + " total = 0\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = int(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n", + " valid_price = True\n", + " except ValueError:\n", + " print(\"Invalid price. Please enter a number.\")\n", + " total += price\n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "ee5fdf30-f075-4a7e-a51f-63220ffc108c", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: e\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price. Please enter a number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price. Please enter a number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: 4\n" + ] + } + ], + "source": [ + "price = total_price(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "ecaca534-7287-4c5b-a62f-d4f2a6628450", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "40e49dde-983a-4e3d-b447-cdbb71a90e80", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_orders = False\n", + " while not valid_orders:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " valid_orders = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " customer_orders = set()\n", + " for i in range(number_of_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\n", + " \"Enter the name of a product that a customer wants to order: \"\n", + " )\n", + " if product not in inventory:\n", + " raise ValueError(\"Product does not exist.\")\n", + " if inventory[product] == 0:\n", + " raise ValueError(\"Product is out of stock.\")\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "8bbba297-b939-4768-bd63-080a9a1ec70e", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: -3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Number of orders cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: w\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'w'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of a product that a customer wants to order: mugrt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product does not exist.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product does not exist.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product is out of stock.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: book\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab691534-093b-40a4-8990-7cbe237cca38", + "metadata": {}, + "outputs": [], + "source": [ + "# 4 # Done" + ] + } + ], + "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..902a603 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,507 @@ "\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": null, + "id": "fbd541e7-ca8c-4a92-a1a8-436b0753339a", + "metadata": {}, + "outputs": [], + "source": [ + "# 1 #" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "1ee3301b-aff2-4c18-a10a-858702770c31", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9b55cd8e-10de-4ac4-a8e5-e874838b7b0a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c826826d-cfdc-4073-84aa-6d0cbeb318ab", + "metadata": {}, + "outputs": [], + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3ba33d52-e055-4908-9b14-13762e5294b6", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 3\n", + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats available: 4\n", + "Enter the quantity of books available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of books available: 4\n", + "Enter the quantity of keychains available: 3\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9df818d0-17a8-45de-af33-4bd120334c1c", + "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": 6, + "id": "889b9468-f2d4-45a2-88f6-fda50c62a85b", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 3\n", + "Enter the quantity of mugs available: 0\n", + "Enter the quantity of hats available: -1\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 hats available: d\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 hats available: 3\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 3\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "markdown", + "id": "db2d70e5-e48e-4221-af3b-66396f36cf11", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "e739efdb-18e8-4d30-b6d3-9dc5c5a73ec1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + "\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " customer_orders = {\n", + " input(\"Enter the name of a product that a customer wants to order: \")\n", + " for i in range(number_of_orders)\n", + " }\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "c751ac82-a58b-4796-8dff-9ebcbc4081fb", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 1\n", + "Enter the name of a product that a customer wants to order: hat\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "3a19a8bf-5668-4da6-94a8-47341846fc91", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'hat'}" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "bd4b9cf0-4e87-4d03-b26a-c942809bdadb", + "metadata": {}, + "outputs": [], + "source": [ + "def total_price(customer_orders):\n", + "\n", + " total = 0\n", + "\n", + " for product in customer_orders:\n", + "\n", + " price = int(input(f\"Enter the price of {product}: \"))\n", + " total += price\n", + "\n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "4212080c-6741-4e4c-b605-3394bd2bb221", + "metadata": {}, + "outputs": [], + "source": [ + "def total_price(customer_orders):\n", + "\n", + " total = 0\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = int(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n", + " valid_price = True\n", + " except ValueError:\n", + " print(\"Invalid price. Please enter a number.\")\n", + " total += price\n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "ee5fdf30-f075-4a7e-a51f-63220ffc108c", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: e\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price. Please enter a number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price. Please enter a number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: 4\n" + ] + } + ], + "source": [ + "price = total_price(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "ecaca534-7287-4c5b-a62f-d4f2a6628450", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "40e49dde-983a-4e3d-b447-cdbb71a90e80", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_orders = False\n", + " while not valid_orders:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " valid_orders = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " customer_orders = set()\n", + " for i in range(number_of_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\n", + " \"Enter the name of a product that a customer wants to order: \"\n", + " )\n", + " if product not in inventory:\n", + " raise ValueError(\"Product does not exist.\")\n", + " if inventory[product] == 0:\n", + " raise ValueError(\"Product is out of stock.\")\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "8bbba297-b939-4768-bd63-080a9a1ec70e", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: -3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Number of orders cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: w\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'w'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of a product that a customer wants to order: mugrt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product does not exist.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product does not exist.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product is out of stock.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: book\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab691534-093b-40a4-8990-7cbe237cca38", + "metadata": {}, + "outputs": [], + "source": [ + "# 4 # Done" + ] } ], "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 +584,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,