Skip to content
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
131 changes: 129 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,138 @@
"\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": "e88770df",
"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(\"Invalid price! Please enter a non-negative value.\")\n",
" valid_price = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" total_price += price\n",
" return total_price"
]
},
{
"cell_type": "markdown",
"id": "c3981370",
"metadata": {},
"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."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "092deebc",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" valid_number = False\n",
" while not valid_number:\n",
" try :\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders < 0:\n",
" raise ValueError(\"inavalid price!please enter a non-negative value.\")\n",
" valid_number = True\n",
" except ValueError as error:\n",
" print(f\"error : {error}\")\n",
"\n",
" customer_orders = set()\n",
" for i in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \")\n",
" if product not in inventory:\n",
" print(f\"Error: '{product}' is out of stock. Please choose a different product.\")\n",
" else:\n",
" customer_orders.add(product)\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1e54577e",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" valid_number = False\n",
" while not valid_number:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders < 0:\n",
" raise ValueError(\"Invalid number of orders! Please enter a non-negative value.\")\n",
" valid_number = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" customer_orders = set()\n",
" for i in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \")\n",
" if product not in inventory:\n",
" print(f\"Error: '{product}' is not a valid product in the inventory. Please enter a valid product name.\")\n",
" elif inventory[product] == 0:\n",
" print(f\"Error: '{product}' is out of stock. Please choose a different product.\")\n",
" else:\n",
" customer_orders.add(product)\n",
" valid_product = True\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "cbc8e13e",
"metadata": {},
"outputs": [],
"source": [
"inventory = {\"apple\": 5, \"banana\": 0, \"orange\": 3}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ddf9434d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'apple'}\n"
]
}
],
"source": [
"orders = get_customer_orders(inventory)\n",
"print(orders)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +217,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down