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
262 changes: 261 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,266 @@
"\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": null,
"id": "02c698d9-b4a8-4618-8046-de780025aa2e",
"metadata": {},
"outputs": [],
"source": [
"#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), \n",
"#display an error message and ask them to re-enter the price for that product.\n",
"\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": 4,
"id": "4b1e3c91-374c-4580-b47e-3d4856e16208",
"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),\n",
"#(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, \n",
"#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": 7,
"id": "1987c819-4af8-4a86-9d87-70c8266da5fc",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "6c930497-056e-45ad-8fa0-bcb4a74ba02c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 4, 'hat': 2, 'book': 2, 'keychain': 0}"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory = {\"t-shirt\" : 5, \"mug\" : 4, \"hat\" : 2, \"book\": 2, \"keychain\" : 0}\n",
"inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "49d8888d-8e1d-40ba-9da2-d9fd9855792c",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 3\n",
"Enter product name: e\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not in inventory\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product name: .\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not in inventory\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product name: t-shirt\n",
"Enter product name: mug\n",
"Enter product name: keychain\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"stock not available\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product name: hat\n"
]
}
],
"source": [
"def get_customer_orders():\n",
" orders_number = 0 \n",
" customer_orders = set()\n",
" \n",
" correct = False\n",
" while not correct:\n",
" try:\n",
" orders_number = int(input(\"Enter the number of customer orders: \"))\n",
" \n",
" if orders_number <=0:\n",
" print(\"Number must be positive\")\n",
" print()\n",
" continue \n",
" correct = True \n",
"\n",
" \n",
" except:\n",
" print(\"Invalid number of orders. Might be a non-numeric or a negative.\")\n",
" print()\n",
"\n",
" for order in range(orders_number):\n",
" correct = False\n",
" while not correct:\n",
" try:\n",
" product = input(\"Enter product name: \")\n",
" \n",
" if product not in inventory:\n",
" raise KeyError\n",
" if inventory[product] <= 0:\n",
" print(\"stock not available\")\n",
" continue\n",
" \n",
"\n",
" customer_orders.add(product)\n",
" correct = True\n",
"\n",
" except KeyError:\n",
" print(\"Product not in inventory\")\n",
"\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders() \n",
"\n",
" \n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "e055f410-28d2-4cf9-ac12-e579506bb1c3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of t-shirt: 5\n",
"Enter the price of hat: 10\n",
"Enter the price of mug: e\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a numerical price\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of mug: -2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please, use a positive price\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of mug: 15\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Price: 30.0\n"
]
}
],
"source": [
"def total_order_price(customer_orders):\n",
" prices = []\n",
" \n",
" for product in customer_orders:\n",
" correct_price = False \n",
"\n",
" while not correct_price:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
" \n",
" if price < 0:\n",
" print(\"Please, use a positive price\")\n",
" print()\n",
" continue \n",
" prices.append(price)\n",
" correct_price = True \n",
" \n",
" except ValueError:\n",
" print(\"Enter a numerical price\")\n",
" print()\n",
"\n",
" total = sum(prices) \n",
" return total\n",
"\n",
"total = total_order_price(customer_orders)\n",
"print(\"Total Price: \", total)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85988886-f3eb-492b-b6b0-d1928b749806",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +350,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down