Skip to content

Error handling lab completed #414

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
187 changes: 185 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,194 @@
"\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": 2,
"id": "559062c5",
"metadata": {},
"outputs": [],
"source": [
"\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"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e2ab90e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a valid quantity.\n"
]
},
{
"data": {
"text/plain": [
"{'mug': 1, 'hat': 2, 'bucket': 3}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"initialize_inventory([\"mug\",\"hat\",\"bucket\"])"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fe21ea23",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(products):\n",
" for product in products:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price_of_each_product = float(input(f\"Enter the price of the {product}:\"))\n",
" if price_of_each_product <= 0:\n",
" raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n",
" valid_price = True\n",
" except ValueError as error:\n",
" print(f\"Error : {error}\")\n",
" price_of_each_product += price_of_each_product\n",
" return price_of_each_product\n",
" \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "677d78b5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error : Invalid quantity! Please enter a non-negative value.\n",
"Error : Invalid quantity! Please enter a non-negative value.\n"
]
},
{
"data": {
"text/plain": [
"10.0"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_total_price([\"mug\",\"hat\",\"bucket\"])"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "6664b41f",
"metadata": {},
"outputs": [],
"source": [
"# 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",
"def get_customer_orders(products):\n",
" customer_order = set()\n",
" num_of_orders = 0\n",
" order = False\n",
" while not order:\n",
" try:\n",
" num_of_orders = int(input(\"Enter the number of orders you want to order: \"))\n",
" if num_of_orders > 0:\n",
" order = 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",
" \n",
" for num in range(num_of_orders):\n",
" product_exists_in_inventory = True\n",
" while product_exists_in_inventory:\n",
" try:\n",
" product_order = input(f\"Enter the product names that you want to order {num+1}:\")\n",
" if product_order in products:\n",
" customer_order.add(product_order)\n",
" product_exists_in_inventory = False\n",
" else:\n",
" print(f\"{product_order} is not in the inventory\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid product.\")\n",
"\n",
" return num_of_orders, customer_order \n",
" \n",
" \n",
" \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "61761938",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 is not in the inventory\n",
"3 is not in the inventory\n",
"3 is not in the inventory\n",
"3 is not in the inventory\n"
]
},
{
"data": {
"text/plain": [
"(2, {'bucket', 'mug'})"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders([\"mug\",\"hat\",\"bucket\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +273,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down