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
130 changes: 128 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,137 @@
"\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": "markdown",
"id": "e5e40ddb",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "1423664e",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (1844852863.py, line 12)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[18], line 12\u001b[0;36m\u001b[0m\n\u001b[0;31m except ValueError:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"def calculate_total_price(customer_orders):\n",
" \n",
" for product in customer_orders:\n",
" while True:\n",
" try:\n",
" price = int(input(\"Enter price of the product: \"))\n",
" if price >= 0:\n",
" customer_orders[product] = price\n",
" break\n",
" else:\n",
" print(\"Price must be non-negative value:\")\n",
" except ValueError:\n",
" print(\"Enter a valid number:\")\n",
"\n",
"prices = { product: float(input(\"Enter the price of product: \")) for product in customer_orders}\n",
" total_price = sum(prices.values())\n",
" return total_price"
]
},
{
"cell_type": "markdown",
"id": "dba58348",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "dcf38fec",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
" valid_num_orders = False\n",
" \n",
" while not valid_num_orders:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders > 0:\n",
" valid_num_orders = True\n",
" else:\n",
" print(\"Number of orders must be a positive integer.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Number of orders must be a positive integer.\")\n",
" \n",
" for _ in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" try:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \")\n",
" if product in inventory and inventory[product] > 0:\n",
" customer_orders.add(product)\n",
" valid_product = True\n",
" else:\n",
" print(\"Invalid product name or out of stock. Please enter a valid product.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid product.\")\n",
" \n",
" return customer_orders\n"
]
},
{
"cell_type": "markdown",
"id": "2c284275",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a43fe9ed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Number of orders must be a positive integer.\n",
"Invalid input. Number of orders must be a positive integer.\n"
]
}
],
"source": [
"inventory = {\"apple\": 5, \"banana\": 3, \"orange\": 0}\n",
"customer_orders = get_customer_orders(inventory)\n",
"print(\"Customer orders:\", customer_orders)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +216,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down