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
309 changes: 309 additions & 0 deletions .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
{
"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": 34,
"id": "29f17f4f-86ee-4924-bd85-fbb343e90c89",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts 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 t-shirts available: 3\n",
"Enter the quantity of mugs available: 5\n",
"Enter the quantity of hats available: 6\n",
"Enter the quantity of books available: 4\n",
"Enter the quantity of keychains available: 2\n"
]
},
{
"data": {
"text/plain": [
"{'t-shirt': 3, 'mug': 5, 'hat': 6, 'book': 4, 'keychain': 2}"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"def initialize_inventory(products):\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",
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "44efb5a8-cdd4-41b0-b415-8f26472281bd",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of products to order: -4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a valid number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of products to order: 2\n",
"Enter the name of a product that a customer wants to order: mug\n",
"Enter the name of a product that a customer wants to order: shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Product does not exist in inventory.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product that a customer wants to order: hat\n"
]
},
{
"data": {
"text/plain": [
"{'hat', 'mug'}"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def get_customer_orders(inventory):\n",
" while True:\n",
" try:\n",
" quantity = int(input(\"Enter the number of products to order: \"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" customer_orders = set()\n",
"\n",
" for i in range(quantity):\n",
" while True:\n",
" try:\n",
" product_name = input(\n",
" \"Enter the name of a product that a customer wants to order: \"\n",
" )\n",
"\n",
" if product_name not in inventory:\n",
" raise ValueError(\"Product does not exist in inventory.\")\n",
" if inventory[product_name] <= 0:\n",
" raise ValueError(\"Product is out of stock.\")\n",
"\n",
" customer_orders.add(product_name)\n",
" break\n",
"\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
" return customer_orders\n",
"get_customer_orders(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "fa49f4b1-c33c-4893-ba1b-d5e4ba090de9",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price for mug: 6\n",
"Enter the price for hat: -4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a valid non-negative number.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price for hat: 5\n"
]
},
{
"data": {
"text/plain": [
"11.0"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def total_price(customer_orders):\n",
" total = 0.0\n",
"\n",
" for product in customer_orders:\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price for {product}: \"))\n",
" \n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
" \n",
" total += price\n",
" break\n",
" \n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" return total\n",
"total_price(customer_orders)"
]
}
],
"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
}
Loading