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
221 changes: 221 additions & 0 deletions .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
{
"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": 24,
"id": "947b315f-432e-464e-a25a-3fcbfce99b88",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (2461498180.py, line 34)",
"output_type": "error",
"traceback": [
" \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[24]\u001b[39m\u001b[32m, line 34\u001b[39m\n\u001b[31m \u001b[39m\u001b[31m```\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n"
]
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 22,
"id": "d28d7403-9d5f-48b0-8598-f11676d596fb",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'inventory' is not defined",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 41\u001b[39m\n\u001b[32m 38\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInvalid input. Please enter a valid number.\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 40\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m orders\n\u001b[32m---> \u001b[39m\u001b[32m41\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInventory:\u001b[39m\u001b[33m\"\u001b[39m, \u001b[43minventory\u001b[49m)\n\u001b[32m 42\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mOrders:\u001b[39m\u001b[33m\"\u001b[39m, orders)\n\u001b[32m 43\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mTotal price:\u001b[39m\u001b[33m\"\u001b[39m, total_price)\n",
"\u001b[31mNameError\u001b[39m: name 'inventory' is not defined"
]
}
],
"source": [
"def get_customer_orders(inventory):\n",
" orders = {}\n",
"\n",
" # Validate number of orders\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of different products you want to order: \"))\n",
" if num_orders > 0:\n",
" break\n",
" else:\n",
" print(\"Number of orders must be greater than 0.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" # Validate each product entry\n",
" for _ in range(num_orders):\n",
" while True:\n",
" product = input(\"Enter the product name: \").lower()\n",
"\n",
" if product not in inventory:\n",
" print(\"Product does not exist. Please enter a valid product.\")\n",
" elif inventory[product] <= 0:\n",
" print(\"Sorry, this product is out of stock. Choose another product.\")\n",
" else:\n",
" break\n",
"\n",
" # Validate quantity\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter quantity of {product}: \"))\n",
" if 0 < quantity <= inventory[product]:\n",
" orders[product] = quantity\n",
" inventory[product] -= quantity\n",
" break\n",
" else:\n",
" print(\"Invalid quantity. Must be positive and within available stock.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" return orders\n",
"print(initialize_inventory) \n",
"print(calculate_total_price)\n",
"print()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "e2d8afde-6721-4556-a297-1c7b1af0ece6",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(orders):\n",
" total_price = 0\n",
"\n",
" for product, quantity in orders.items():\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
" if price >= 0:\n",
" total_price += price * quantity\n",
" valid_input = True\n",
" else:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value.\")\n",
"\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36352fa8-f89a-4181-9025-93c3e8b199dc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b3b3757-9d38-477a-a5e8-9ad3b0479895",
"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.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added anaconda_projects/db/project_filebrowser.db
Binary file not shown.
125 changes: 124 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,129 @@
"\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": 24,
"id": "947b315f-432e-464e-a25a-3fcbfce99b88",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (2461498180.py, line 34)",
"output_type": "error",
"traceback": [
" \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[24]\u001b[39m\u001b[32m, line 34\u001b[39m\n\u001b[31m \u001b[39m\u001b[31m```\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n"
]
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 22,
"id": "d28d7403-9d5f-48b0-8598-f11676d596fb",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'inventory' is not defined",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 41\u001b[39m\n\u001b[32m 38\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInvalid input. Please enter a valid number.\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 40\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m orders\n\u001b[32m---> \u001b[39m\u001b[32m41\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInventory:\u001b[39m\u001b[33m\"\u001b[39m, \u001b[43minventory\u001b[49m)\n\u001b[32m 42\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mOrders:\u001b[39m\u001b[33m\"\u001b[39m, orders)\n\u001b[32m 43\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mTotal price:\u001b[39m\u001b[33m\"\u001b[39m, total_price)\n",
"\u001b[31mNameError\u001b[39m: name 'inventory' is not defined"
]
}
],
"source": [
"def get_customer_orders(inventory):\n",
" orders = {}\n",
"\n",
" # Validate number of orders\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of different products you want to order: \"))\n",
" if num_orders > 0:\n",
" break\n",
" else:\n",
" print(\"Number of orders must be greater than 0.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" # Validate each product entry\n",
" for _ in range(num_orders):\n",
" while True:\n",
" product = input(\"Enter the product name: \").lower()\n",
"\n",
" if product not in inventory:\n",
" print(\"Product does not exist. Please enter a valid product.\")\n",
" elif inventory[product] <= 0:\n",
" print(\"Sorry, this product is out of stock. Choose another product.\")\n",
" else:\n",
" break\n",
"\n",
" # Validate quantity\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter quantity of {product}: \"))\n",
" if 0 < quantity <= inventory[product]:\n",
" orders[product] = quantity\n",
" inventory[product] -= quantity\n",
" break\n",
" else:\n",
" print(\"Invalid quantity. Must be positive and within available stock.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" return orders\n",
"print(initialize_inventory) \n",
"print(calculate_total_price)\n",
"print()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "e2d8afde-6721-4556-a297-1c7b1af0ece6",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(orders):\n",
" total_price = 0\n",
"\n",
" for product, quantity in orders.items():\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
" if price >= 0:\n",
" total_price += price * quantity\n",
" valid_input = True\n",
" else:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value.\")\n",
"\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36352fa8-f89a-4181-9025-93c3e8b199dc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b3b3757-9d38-477a-a5e8-9ad3b0479895",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +213,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down