Skip to content
Open
Show file tree
Hide file tree
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
98 changes: 98 additions & 0 deletions .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"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"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
247 changes: 246 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,251 @@
"\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": 11,
"id": "60831e74-f68e-4c91-a0c1-e3941e297c6d",
"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": "markdown",
"id": "d16087c2-ca85-4329-ad8c-01775c74281e",
"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": 12,
"id": "7d1575bd-e87f-4ac3-ba1d-8866baafb52e",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
" for product in customer_orders:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
" total_price += price\n",
" valid_price = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}. Please enter a valid non-negative number.\")\n",
" return total_price"
]
},
{
"cell_type": "markdown",
"id": "e2bb175b-ee24-4d01-96ac-209dc40927ab",
"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"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "70724991-6c8a-42f8-abcd-335a0dd95892",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
"\n",
" valid_count = False\n",
" while not valid_count:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders >= 0:\n",
" valid_count = True\n",
" else:\n",
" print(\"Invalid input! Please enter a non-negative number.\")\n",
" except ValueError:\n",
" print(\"Invalid input! Please enter a valid number.\")\n",
"\n",
" while len(customer_orders) < num_orders:\n",
" product = input(f\"Enter product {len(customer_orders) + 1} of {num_orders}: \").strip().lower()\n",
"\n",
" if product not in inventory:\n",
" print(f\"Error: '{product}' is not in the inventory. Please try again.\")\n",
" elif inventory[product] <= 0:\n",
" print(f\"Error: '{product}' is out of stock. Please try again.\")\n",
" else:\n",
" customer_orders.add(product)\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "markdown",
"id": "0dafacce-274f-4f6c-8858-8cecfd24da2b",
"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": 14,
"id": "ea881212-b719-4985-87d7-e402ef9978c0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: -5\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 t-shirts available: 0\n",
"Enter the quantity of mugs available: 2\n",
"Enter the quantity of hats available: 0\n",
"Enter the quantity of books available: 2\n",
"Enter the quantity of keychains available: 3\n",
"Enter the number of customer orders: 2\n",
"Enter product 1 of 2: hi\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: 'hi' is not in the inventory. Please try again.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product 1 of 2: t-shirts\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: 't-shirts' is not in the inventory. Please try again.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product 1 of 2: t-shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: 't-shirt' is out of stock. Please try again.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product 1 of 2: MUG\n",
"Enter product 2 of 2: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: 'hat' is out of stock. Please try again.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product 2 of 2: keychain\n",
"Enter the price of keychain: -8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Price cannot be negative.. Please enter a valid non-negative number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of keychain: 15.99\n",
"Enter the price of mug: 8.99\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Total Price: 24.98\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"total_price = calculate_total_price(customer_orders)\n",
"\n",
"print(f\"\\nTotal Price: {total_price: }\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "568bfb70-0bef-48b9-aca5-74f1039b4038",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +335,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.7"
}
},
"nbformat": 4,
Expand Down