Skip to content

lab complete #416

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
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
}
181 changes: 178 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,188 @@
"\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": 9,
"id": "5d45a43a-e162-4107-a7d1-bbb38e3d0cb2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 3\n",
"Enter the quantity of mugs available: 3\n",
"Enter the quantity of hats available: 3\n",
"Enter the quantity of books available: 3\n",
"Enter the quantity of keychains available: 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initialized Inventory: {'t-shirt': 3, 'mug': 3, 'hat': 3, 'book': 3, 'keychain': 3}\n"
]
}
],
"source": [
"# 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",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"print(\"Initialized Inventory:\", inventory)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "716737f9-4dce-41db-ac16-feecae269780",
"metadata": {},
"outputs": [],
"source": [
"# 2\n",
"\n",
"def calculate_total_price(products):\n",
" total_price = 0.0\n",
" for product in products:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(\"Enter the price for {product}: \"))\n",
" \n",
" if price < 0:\n",
" raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n",
" \n",
"\n",
" valid_price = True\n",
" \n",
" except ValueError as error:\n",
" print(\"Error: {error}. Please enter a valid, non-negative price.\")\n",
" return total_price\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "d93e2343-3454-4992-b954-bfae67c536f9",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: f\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: invalid literal for int() with base 10: 'f'\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: -2\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: 33\n",
"Enter the quantity of hats available: 3\n",
"Enter the quantity of books available: 3\n",
"Enter the quantity of keychains available: 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initialized Inventory: {'t-shirt': 3, 'mug': 33, 'hat': 3, 'book': 3, 'keychain': 3}\n"
]
}
],
"source": [
"# 3\n",
"def get_customer_orders(inventory):\n",
" customer_orders = {}\n",
" \n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of products you wish to order: \"))\n",
" \n",
" if num_orders <= 0:\n",
" raise ValueError(\"Number of orders must be a positive integer.\")\n",
" \n",
" for _ in range(num_orders):\n",
" while True:\n",
" product_name = input(\"Enter the name of the product you want to order: \").strip()\n",
" \n",
" # Check if product is in inventory and has stock available\n",
" if product_name in inventory and inventory[product_name] > 0:\n",
" # Prompt for quantity\n",
" quantity = int(input(f\"Enter the quantity of {product_name} you wish to order: \"))\n",
"\n",
" if quantity > inventory[product_name]:\n",
"\n",
" customer_orders[product_name] = quantity\n",
" break\n",
" else:\n",
" print(\"Invalid product name or out of stock. Please enter a valid product.\")\n",
" break\n",
" \n",
" except ValueError as error:\n",
" print(f\"Error: {error}. Please enter a valid number.\")\n",
"\n",
" return customer_orders\n",
" \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"print(\"Initialized Inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7dee187-4033-41bf-be6c-a565bf39d3f6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -90,7 +265,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down