From 0cffdf1cdcb8fd78d42cb1050322cfb81dd873bc Mon Sep 17 00:00:00 2001 From: J0hnrusso Date: Tue, 3 Dec 2024 09:42:56 +0000 Subject: [PATCH 1/2] lab done --- ...lab-python-error-handling-checkpoint.ipynb | 276 ++++++++++++++++++ lab-python-error-handling.ipynb | 180 +++++++++++- 2 files changed, 455 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..c07243d --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,276 @@ +{ + "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": 6, + "id": "ee849abd-4151-4884-8f5a-9caa5fd46c4f", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "total_products_ordered = ()\n", + "percentage_ordered = ()\n", + "inventory = {}\n", + "customer_orders = {}\n", + "\n", + "#1\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", + "#2\n", + "def get_customer_orders(products):\n", + " customer_orders = set()\n", + " print(\"\\nEnter the names of the products the customer wants to order.\")\n", + " while True:\n", + " product_name = input(\"Enter a product name: \").strip().lower()\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " print(f\"'{product_name}' added to the order.\")\n", + " else:\n", + " print(f\"'{product_name}' is not a valid product. Please choose from {products}.\")\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != \"yes\":\n", + " break\n", + " return customer_orders\n", + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " if inventory[product] == 0:\n", + " print(f\"'{product}' is now out of stock.\")\n", + " else:\n", + " print(f\"'{product} doesn't exist or is out of stock.\")\n", + " return inventory\n", + "\n", + "#4\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "#5\n", + "def print_order_statistics(total_products_ordered, percentage_ordered):\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n", + "\n", + "#6\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3dc0726d-11ea-4b53-8af6-23629c243962", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 5\n", + "Enter the quantity of books available: 0\n", + "Enter the quantity of keychains available: -5\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 keychains available: l\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'l'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of keychains available: ooo\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'ooo'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of keychains available: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Enter the names of the products the customer wants to order.\n" + ] + }, + { + "ename": "KeyboardInterrupt", + "evalue": "Interrupted by user", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[8], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m inventory \u001b[38;5;241m=\u001b[39m initialize_inventory(products)\n\u001b[1;32m----> 3\u001b[0m customer_orders \u001b[38;5;241m=\u001b[39m get_customer_orders(products)\n\u001b[0;32m 5\u001b[0m inventory \u001b[38;5;241m=\u001b[39m update_inventory(customer_orders, inventory)\n\u001b[0;32m 7\u001b[0m total_products_ordered, percentage_ordered \u001b[38;5;241m=\u001b[39m calculate_order_statistics(customer_orders, products)\n", + "Cell \u001b[1;32mIn[6], line 28\u001b[0m, in \u001b[0;36mget_customer_orders\u001b[1;34m(products)\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mEnter the names of the products the customer wants to order.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 27\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m---> 28\u001b[0m product_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a product name: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mstrip()\u001b[38;5;241m.\u001b[39mlower()\n\u001b[0;32m 29\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m product_name \u001b[38;5;129;01min\u001b[39;00m products:\n\u001b[0;32m 30\u001b[0m customer_orders\u001b[38;5;241m.\u001b[39madd(product_name)\n", + "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1262\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 1260\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 1261\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[1;32m-> 1262\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_input_request(\n\u001b[0;32m 1263\u001b[0m \u001b[38;5;28mstr\u001b[39m(prompt),\n\u001b[0;32m 1264\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parent_ident[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[0;32m 1265\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_parent(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m 1266\u001b[0m password\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 1267\u001b[0m )\n", + "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1305\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 1302\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m 1303\u001b[0m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[0;32m 1304\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m-> 1305\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1306\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m 1307\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", + "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" + ] + } + ], + "source": [ + " inventory = initialize_inventory(products)\n", + "\n", + " customer_orders = get_customer_orders(products)\n", + "\n", + " inventory = update_inventory(customer_orders, inventory)\n", + "\n", + " total_products_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n", + "\n", + " print_order_statistics(total_products_ordered, percentage_ordered)\n", + "\n", + " print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6f6daced-59f3-4d15-936a-bc15c134b49f", + "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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..c07243d 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,184 @@ "\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": 6, + "id": "ee849abd-4151-4884-8f5a-9caa5fd46c4f", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "total_products_ordered = ()\n", + "percentage_ordered = ()\n", + "inventory = {}\n", + "customer_orders = {}\n", + "\n", + "#1\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", + "#2\n", + "def get_customer_orders(products):\n", + " customer_orders = set()\n", + " print(\"\\nEnter the names of the products the customer wants to order.\")\n", + " while True:\n", + " product_name = input(\"Enter a product name: \").strip().lower()\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " print(f\"'{product_name}' added to the order.\")\n", + " else:\n", + " print(f\"'{product_name}' is not a valid product. Please choose from {products}.\")\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != \"yes\":\n", + " break\n", + " return customer_orders\n", + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " if inventory[product] == 0:\n", + " print(f\"'{product}' is now out of stock.\")\n", + " else:\n", + " print(f\"'{product} doesn't exist or is out of stock.\")\n", + " return inventory\n", + "\n", + "#4\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "#5\n", + "def print_order_statistics(total_products_ordered, percentage_ordered):\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n", + "\n", + "#6\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3dc0726d-11ea-4b53-8af6-23629c243962", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 5\n", + "Enter the quantity of books available: 0\n", + "Enter the quantity of keychains available: -5\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 keychains available: l\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'l'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of keychains available: ooo\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'ooo'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of keychains available: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Enter the names of the products the customer wants to order.\n" + ] + }, + { + "ename": "KeyboardInterrupt", + "evalue": "Interrupted by user", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[8], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m inventory \u001b[38;5;241m=\u001b[39m initialize_inventory(products)\n\u001b[1;32m----> 3\u001b[0m customer_orders \u001b[38;5;241m=\u001b[39m get_customer_orders(products)\n\u001b[0;32m 5\u001b[0m inventory \u001b[38;5;241m=\u001b[39m update_inventory(customer_orders, inventory)\n\u001b[0;32m 7\u001b[0m total_products_ordered, percentage_ordered \u001b[38;5;241m=\u001b[39m calculate_order_statistics(customer_orders, products)\n", + "Cell \u001b[1;32mIn[6], line 28\u001b[0m, in \u001b[0;36mget_customer_orders\u001b[1;34m(products)\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mEnter the names of the products the customer wants to order.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 27\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m---> 28\u001b[0m product_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a product name: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mstrip()\u001b[38;5;241m.\u001b[39mlower()\n\u001b[0;32m 29\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m product_name \u001b[38;5;129;01min\u001b[39;00m products:\n\u001b[0;32m 30\u001b[0m customer_orders\u001b[38;5;241m.\u001b[39madd(product_name)\n", + "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1262\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 1260\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 1261\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[1;32m-> 1262\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_input_request(\n\u001b[0;32m 1263\u001b[0m \u001b[38;5;28mstr\u001b[39m(prompt),\n\u001b[0;32m 1264\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parent_ident[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[0;32m 1265\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_parent(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m 1266\u001b[0m password\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 1267\u001b[0m )\n", + "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1305\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 1302\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m 1303\u001b[0m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[0;32m 1304\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m-> 1305\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1306\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m 1307\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", + "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" + ] + } + ], + "source": [ + " inventory = initialize_inventory(products)\n", + "\n", + " customer_orders = get_customer_orders(products)\n", + "\n", + " inventory = update_inventory(customer_orders, inventory)\n", + "\n", + " total_products_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n", + "\n", + " print_order_statistics(total_products_ordered, percentage_ordered)\n", + "\n", + " print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6f6daced-59f3-4d15-936a-bc15c134b49f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +268,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, From a8cf9a5e1ef48eb297ef8661f8f52e8858263919 Mon Sep 17 00:00:00 2001 From: J0hnrusso Date: Mon, 16 Dec 2024 14:40:09 +0000 Subject: [PATCH 2/2] revison lab --- ...lab-python-error-handling-checkpoint.ipynb | 162 +++++++++++++----- lab-python-error-handling.ipynb | 162 +++++++++++++----- 2 files changed, 230 insertions(+), 94 deletions(-) diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb index c07243d..3bfc264 100644 --- a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -75,12 +75,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "ee849abd-4151-4884-8f5a-9caa5fd46c4f", "metadata": {}, "outputs": [], "source": [ - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "total_products_ordered = ()\n", "percentage_ordered = ()\n", "inventory = {}\n", @@ -102,21 +101,6 @@ " inventory[product] = quantity\n", " return inventory\n", "\n", - "#2\n", - "def get_customer_orders(products):\n", - " customer_orders = set()\n", - " print(\"\\nEnter the names of the products the customer wants to order.\")\n", - " while True:\n", - " product_name = input(\"Enter a product name: \").strip().lower()\n", - " if product_name in products:\n", - " customer_orders.add(product_name)\n", - " print(f\"'{product_name}' added to the order.\")\n", - " else:\n", - " print(f\"'{product_name}' is not a valid product. Please choose from {products}.\")\n", - " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", - " if another != \"yes\":\n", - " break\n", - " return customer_orders\n", "#3\n", "def update_inventory(customer_orders, inventory):\n", " for product in customer_orders:\n", @@ -136,20 +120,88 @@ "\n", "#5\n", "def print_order_statistics(total_products_ordered, percentage_ordered):\n", - " print(\"\\nOrder Statistics:\")\n", + " print(\"Order Statistics:\")\n", " print(f\"Total Products Ordered: {total_products_ordered}\")\n", " print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n", "\n", "#6\n", "def print_updated_inventory(inventory):\n", - " print(\"\\nUpdated Inventory:\")\n", + " print(\"Updated Inventory:\")\n", " for product, quantity in inventory.items():\n", - " print(f\"{product}: {quantity}\")\n" + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "id": "0cc7a9b7-3810-45a8-9918-da5ffcfae62c", + "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": 8, + "id": "6c2c2f83-0849-481c-9408-f95357617e89", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def get_customer_orders(products):\n", + " customer_orders = set() \n", + " try:\n", + " num_products = int(input(\"How many products does the customer want to order? \").strip())\n", + " if num_products < 0:\n", + " raise ValueError(\"The number of products cannot be negative.\")\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " return customer_orders\n", + " \n", + " print(\"\\nEnter the names of the products the customer wants to order.\")\n", + " for _ in range(num_products):\n", + " while True:\n", + " product_name = input(\"Enter a product name: \").strip().lower()\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " print(f\"'{product_name}' added to the order.\")\n", + " break\n", + " else:\n", + " print(f\"'{product_name}' is not a valid product. Please choose from {products}.\")\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d0569626-0acf-46be-ac90-93e7fc52da8b", + "metadata": {}, + "outputs": [], + "source": [ + "#7\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"enter the prize of {product}\"))\n", + " if price >= 0:\n", + " total_price += price\n", + " valid_input = True\n", + " else:\n", + " print(\"the price cannot be negative!\")\n", + " except ValueError:\n", + " print(f\"Error: invalid input - {error}\")\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, "id": "3dc0726d-11ea-4b53-8af6-23629c243962", "metadata": {}, "outputs": [ @@ -160,87 +212,103 @@ "Enter the quantity of t-shirts available: 5\n", "Enter the quantity of mugs available: 5\n", "Enter the quantity of hats available: 5\n", - "Enter the quantity of books available: 0\n", - "Enter the quantity of keychains available: -5\n" + "Enter the quantity of books available: 5\n", + "Enter the quantity of keychains available: 5\n", + "How many products does the customer want to order? 2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Error: Invalid quantity! Please enter a non-negative value.\n" + "\n", + "Enter the names of the products the customer wants to order.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of keychains available: l\n" + "Enter a product name: book\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Error: invalid literal for int() with base 10: 'l'\n" + "'book' added to the order.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of keychains available: ooo\n" + "Enter a product name: mmug\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Error: invalid literal for int() with base 10: 'ooo'\n" + "'mmug' is not a valid product. Please choose from {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of keychains available: 5\n" + "Enter a product name: mug\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "\n", - "Enter the names of the products the customer wants to order.\n" + "'mug' added to the order.\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.00%\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 5\n", + "book: 4\n", + "keychain: 5\n" ] }, { - "ename": "KeyboardInterrupt", - "evalue": "Interrupted by user", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[8], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m inventory \u001b[38;5;241m=\u001b[39m initialize_inventory(products)\n\u001b[1;32m----> 3\u001b[0m customer_orders \u001b[38;5;241m=\u001b[39m get_customer_orders(products)\n\u001b[0;32m 5\u001b[0m inventory \u001b[38;5;241m=\u001b[39m update_inventory(customer_orders, inventory)\n\u001b[0;32m 7\u001b[0m total_products_ordered, percentage_ordered \u001b[38;5;241m=\u001b[39m calculate_order_statistics(customer_orders, products)\n", - "Cell \u001b[1;32mIn[6], line 28\u001b[0m, in \u001b[0;36mget_customer_orders\u001b[1;34m(products)\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mEnter the names of the products the customer wants to order.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 27\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m---> 28\u001b[0m product_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a product name: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mstrip()\u001b[38;5;241m.\u001b[39mlower()\n\u001b[0;32m 29\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m product_name \u001b[38;5;129;01min\u001b[39;00m products:\n\u001b[0;32m 30\u001b[0m customer_orders\u001b[38;5;241m.\u001b[39madd(product_name)\n", - "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1262\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 1260\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 1261\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[1;32m-> 1262\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_input_request(\n\u001b[0;32m 1263\u001b[0m \u001b[38;5;28mstr\u001b[39m(prompt),\n\u001b[0;32m 1264\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parent_ident[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[0;32m 1265\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_parent(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m 1266\u001b[0m password\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 1267\u001b[0m )\n", - "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1305\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 1302\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m 1303\u001b[0m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[0;32m 1304\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m-> 1305\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1306\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m 1307\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", - "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" + "name": "stdin", + "output_type": "stream", + "text": [ + "enter the prize of book 10\n", + "enter the prize of mug 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total price: 12.0\n" ] } ], "source": [ - " inventory = initialize_inventory(products)\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", "\n", - " customer_orders = get_customer_orders(products)\n", + "inventory = update_inventory(customer_orders, inventory)\n", "\n", - " inventory = update_inventory(customer_orders, inventory)\n", + "total_products_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n", "\n", - " total_products_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(total_products_ordered, percentage_ordered)\n", "\n", - " print_order_statistics(total_products_ordered, percentage_ordered)\n", + "print_updated_inventory(inventory)\n", "\n", - " print_updated_inventory(inventory)\n" + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"total price: {total_price}\")\n" ] }, { diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index c07243d..3bfc264 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,12 +75,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "ee849abd-4151-4884-8f5a-9caa5fd46c4f", "metadata": {}, "outputs": [], "source": [ - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "total_products_ordered = ()\n", "percentage_ordered = ()\n", "inventory = {}\n", @@ -102,21 +101,6 @@ " inventory[product] = quantity\n", " return inventory\n", "\n", - "#2\n", - "def get_customer_orders(products):\n", - " customer_orders = set()\n", - " print(\"\\nEnter the names of the products the customer wants to order.\")\n", - " while True:\n", - " product_name = input(\"Enter a product name: \").strip().lower()\n", - " if product_name in products:\n", - " customer_orders.add(product_name)\n", - " print(f\"'{product_name}' added to the order.\")\n", - " else:\n", - " print(f\"'{product_name}' is not a valid product. Please choose from {products}.\")\n", - " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", - " if another != \"yes\":\n", - " break\n", - " return customer_orders\n", "#3\n", "def update_inventory(customer_orders, inventory):\n", " for product in customer_orders:\n", @@ -136,20 +120,88 @@ "\n", "#5\n", "def print_order_statistics(total_products_ordered, percentage_ordered):\n", - " print(\"\\nOrder Statistics:\")\n", + " print(\"Order Statistics:\")\n", " print(f\"Total Products Ordered: {total_products_ordered}\")\n", " print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n", "\n", "#6\n", "def print_updated_inventory(inventory):\n", - " print(\"\\nUpdated Inventory:\")\n", + " print(\"Updated Inventory:\")\n", " for product, quantity in inventory.items():\n", - " print(f\"{product}: {quantity}\")\n" + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "id": "0cc7a9b7-3810-45a8-9918-da5ffcfae62c", + "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": 8, + "id": "6c2c2f83-0849-481c-9408-f95357617e89", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def get_customer_orders(products):\n", + " customer_orders = set() \n", + " try:\n", + " num_products = int(input(\"How many products does the customer want to order? \").strip())\n", + " if num_products < 0:\n", + " raise ValueError(\"The number of products cannot be negative.\")\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " return customer_orders\n", + " \n", + " print(\"\\nEnter the names of the products the customer wants to order.\")\n", + " for _ in range(num_products):\n", + " while True:\n", + " product_name = input(\"Enter a product name: \").strip().lower()\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " print(f\"'{product_name}' added to the order.\")\n", + " break\n", + " else:\n", + " print(f\"'{product_name}' is not a valid product. Please choose from {products}.\")\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d0569626-0acf-46be-ac90-93e7fc52da8b", + "metadata": {}, + "outputs": [], + "source": [ + "#7\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"enter the prize of {product}\"))\n", + " if price >= 0:\n", + " total_price += price\n", + " valid_input = True\n", + " else:\n", + " print(\"the price cannot be negative!\")\n", + " except ValueError:\n", + " print(f\"Error: invalid input - {error}\")\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, "id": "3dc0726d-11ea-4b53-8af6-23629c243962", "metadata": {}, "outputs": [ @@ -160,87 +212,103 @@ "Enter the quantity of t-shirts available: 5\n", "Enter the quantity of mugs available: 5\n", "Enter the quantity of hats available: 5\n", - "Enter the quantity of books available: 0\n", - "Enter the quantity of keychains available: -5\n" + "Enter the quantity of books available: 5\n", + "Enter the quantity of keychains available: 5\n", + "How many products does the customer want to order? 2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Error: Invalid quantity! Please enter a non-negative value.\n" + "\n", + "Enter the names of the products the customer wants to order.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of keychains available: l\n" + "Enter a product name: book\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Error: invalid literal for int() with base 10: 'l'\n" + "'book' added to the order.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of keychains available: ooo\n" + "Enter a product name: mmug\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Error: invalid literal for int() with base 10: 'ooo'\n" + "'mmug' is not a valid product. Please choose from {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of keychains available: 5\n" + "Enter a product name: mug\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "\n", - "Enter the names of the products the customer wants to order.\n" + "'mug' added to the order.\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.00%\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 5\n", + "book: 4\n", + "keychain: 5\n" ] }, { - "ename": "KeyboardInterrupt", - "evalue": "Interrupted by user", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[8], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m inventory \u001b[38;5;241m=\u001b[39m initialize_inventory(products)\n\u001b[1;32m----> 3\u001b[0m customer_orders \u001b[38;5;241m=\u001b[39m get_customer_orders(products)\n\u001b[0;32m 5\u001b[0m inventory \u001b[38;5;241m=\u001b[39m update_inventory(customer_orders, inventory)\n\u001b[0;32m 7\u001b[0m total_products_ordered, percentage_ordered \u001b[38;5;241m=\u001b[39m calculate_order_statistics(customer_orders, products)\n", - "Cell \u001b[1;32mIn[6], line 28\u001b[0m, in \u001b[0;36mget_customer_orders\u001b[1;34m(products)\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mEnter the names of the products the customer wants to order.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 27\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m---> 28\u001b[0m product_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a product name: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mstrip()\u001b[38;5;241m.\u001b[39mlower()\n\u001b[0;32m 29\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m product_name \u001b[38;5;129;01min\u001b[39;00m products:\n\u001b[0;32m 30\u001b[0m customer_orders\u001b[38;5;241m.\u001b[39madd(product_name)\n", - "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1262\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 1260\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 1261\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[1;32m-> 1262\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_input_request(\n\u001b[0;32m 1263\u001b[0m \u001b[38;5;28mstr\u001b[39m(prompt),\n\u001b[0;32m 1264\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parent_ident[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[0;32m 1265\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_parent(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m 1266\u001b[0m password\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 1267\u001b[0m )\n", - "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1305\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 1302\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m 1303\u001b[0m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[0;32m 1304\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m-> 1305\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1306\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m 1307\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", - "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" + "name": "stdin", + "output_type": "stream", + "text": [ + "enter the prize of book 10\n", + "enter the prize of mug 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total price: 12.0\n" ] } ], "source": [ - " inventory = initialize_inventory(products)\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", "\n", - " customer_orders = get_customer_orders(products)\n", + "inventory = update_inventory(customer_orders, inventory)\n", "\n", - " inventory = update_inventory(customer_orders, inventory)\n", + "total_products_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n", "\n", - " total_products_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(total_products_ordered, percentage_ordered)\n", "\n", - " print_order_statistics(total_products_ordered, percentage_ordered)\n", + "print_updated_inventory(inventory)\n", "\n", - " print_updated_inventory(inventory)\n" + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"total price: {total_price}\")\n" ] }, {