Skip to content

lab done #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
362 changes: 314 additions & 48 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"cell_type": "code",
"execution_count": 3,
"id": "6df7713f-b108-42aa-ab43-61085202d73c",
"metadata": {},
"outputs": [],
"source": [
"# Lab | Error Handling"
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"price_list={'t-shirt':20, 'mug':9.95, 'hat':5.95, 'book':25, 'keychain':5}"
]
},
{
"cell_type": "markdown",
"id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b",
"cell_type": "code",
"execution_count": 4,
"id": "cb1dc152-b1fa-4334-a80d-3d7332fdec35",
"metadata": {},
"outputs": [],
"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",
Expand All @@ -36,41 +31,312 @@
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"# Or, in another way:\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c30b7bd7-4c7c-41f5-a25e-62799f94c29f",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 50\n",
"Enter the quantity of mugs available: 40\n",
"Enter the quantity of hats available: 30\n",
"Enter the quantity of books available: 60\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: 45\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b73f59e4-e7a5-42e7-a56d-e1c72c156a72",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory: {'t-shirt': 50, 'mug': 40, 'hat': 30, 'book': 60, 'keychain': 45}\n"
]
}
],
"source": [
"print(\"Inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "76f436c8-3481-446c-b2bd-a19e16a0f72e",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" while True:\n",
" try:\n",
" customer_orders = int(input(\"How many different products do you want to order? \"))\n",
" if customer_orders < 1:\n",
" print(\"Please enter a number greater than 0.\")\n",
" continue\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_input = False\n",
" while not valid_input:\n",
" orders = set()\n",
" for i in range(customer_orders):\n",
" while True:\n",
" product = input(f\"Enter the name of product #{i + 1}: \").strip().lower()\n",
" if not product:\n",
" print(\"Product name cannot be empty.\")\n",
" elif product not in products:\n",
" print(f\"'{product}' is not available. Please choose from the available products:\")\n",
" print(\", \".join(sorted(products)))\n",
" else:\n",
" orders.add(product)\n",
" break\n",
" return orders"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "41d5b969-9c65-4316-9a8b-1fb4d71152f8",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many different products do you want to order? 3\n",
"Enter the name of product #1: book\n",
"Enter the name of product #2: t-shirt\n",
"Enter the name of product #3: gum\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'gum' is not available. Please choose from the available products:\n",
"book, hat, keychain, mug, t-shirt\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of product #3: mug\n"
]
}
],
"source": [
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "00dd20eb-3881-4725-9656-4e34d5c6611f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer Orders: {'mug', 't-shirt', 'book'}\n"
]
}
],
"source": [
"print(\"Customer Orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "7b811ba6-8349-485a-b083-d8f9f27671a7",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total = 0.0\n",
" for product in customer_orders:\n",
" while True:\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",
" price_input = input(f\"Enter the price for {product}: \")\n",
" price = float(price_input)\n",
" if price < 0:\n",
" print(\"Price cannot be negative. Please enter a valid amount.\")\n",
" continue\n",
" total += price\n",
" break\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"
" print(\"Invalid input. Please enter a numeric value.\")\n",
" return total"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "28a36945-e739-4a5e-b85d-a79b9df9ba4d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price for mug: 9.95\n",
"Enter the price for t-shirt: 20\n",
"Enter the price for book: 25\n"
]
}
],
"source": [
"total_price = calculate_total_price(customer_orders) "
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "933ac805-5bd2-4404-94f1-15ad9b5215be",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_stats(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_of_products_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, int(percentage_of_products_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "c20d7638-fe07-4f57-aa07-9084fe55cf4d",
"metadata": {},
"outputs": [],
"source": [
"total_products_ordered, percentage_of_products_ordered = calculate_order_stats(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "579f2095-3eed-438b-a611-49c09823ebdf",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(inventory, customer_orders):\n",
" for item in customer_orders:\n",
" if item in inventory and inventory[item] > 0:\n",
" inventory[item] -= 1\n",
" return {product: qty for product, qty in inventory.items() if qty > 0}"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "105b7769-4c83-4fd4-9f03-081429be13a2",
"metadata": {},
"outputs": [],
"source": [
"inventory = update_inventory(inventory, customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "8b521f9a-99c1-4999-9ef3-eee7ba62641e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory: {'t-shirt': 49, 'mug': 39, 'hat': 30, 'book': 59, 'keychain': 45}\n"
]
}
],
"source": [
"print(\"Updated Inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "337b7b5f-220a-445c-b845-b06a443fc10e",
"metadata": {},
"outputs": [],
"source": [
"def print_order_stats(total_products_ordered, percentage_of_products_ordered):\n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", total_products_ordered)\n",
" print(\"Percentage of Products Ordered:\", int(percentage_of_products_ordered), \"%\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "b584b64a-4de8-4285-a24a-dfdb7a09b859",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "953c6e26-d14a-4520-87ba-476fa256fa35",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60 %\n",
"Updated Inventory:\n",
"t-shirt: 49\n",
"mug: 39\n",
"hat: 30\n",
"book: 59\n",
"keychain: 45\n",
"Total Price: 54.95\n"
]
}
],
"source": [
"print_order_stats(total_products_ordered, percentage_of_products_ordered)\n",
"print_updated_inventory(inventory)\n",
"print(\"Total Price:\", total_price)"
]
}
],
Expand All @@ -90,7 +356,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down