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
317 changes: 314 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,324 @@
"\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": 1,
"id": "fc75f433-a5d5-4ae6-87a0-eefa9a309368",
"metadata": {},
"outputs": [],
"source": [
"# Step 1: Define the function for initializing the inventory with error handling\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} 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": 2,
"id": "5d73c1d5-73d4-44df-8eb0-7fb9fd5aee38",
"metadata": {},
"outputs": [],
"source": [
"# Step 2: Modify the calculate_total_price function to include error handling.\n",
"\n",
"def total_price(customer_orders):\n",
" for product in customer_orders:\n",
" valid_price = False\n",
" prices = {}\n",
" while not valid_price:\n",
" price = input(f\"Please enter the price of the {product} you ordered: \")\n",
" try:\n",
" price = int(price)\n",
" if price < 0:\n",
" print(\"Price cannot be negative. Please enter a valid price: \")\n",
" else:\n",
" prices[product] = price\n",
" valid_price = True\n",
" except:\n",
" print(\"invalid input. Please enter a numerical value: \")\n",
" \n",
" total_to_pay = sum(prices.values()) \n",
" return total_to_pay"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "26f6d374-334c-4123-aa7a-1482ee2e3c87",
"metadata": {},
"outputs": [],
"source": [
"# Step 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",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" valid_number = False\n",
" while not valid_number:\n",
" number_of_orders = input(\"Please enter the amount of unique products you wish to order: \")\n",
" try:\n",
" number_of_orders = int(number_of_orders)\n",
" if number_of_orders < 0 or number_of_orders > len(products):\n",
" print(\"Please enter an amount that is a non-negative, as well as, not bigger than the amount of unique products we have available\")\n",
" else:\n",
" for i in range(number_of_orders):\n",
" valid_name = False\n",
" while not valid_name:\n",
" try:\n",
" product_name = input(\"Please enter the name of a product you wish to order: \")\n",
" if product_name in products:\n",
" customer_orders.add(product_name)\n",
" valid_name = True\n",
" else:\n",
" print(\"Please enter a product that is available in our shop: \")\n",
" except:\n",
" print(\"Invalid input, please enter an available product: \")\n",
" valid_number = True\n",
" except ValueError:\n",
" print(\"Please enter an numeric value: \")\n",
"\n",
" return(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "33021512-ecc9-4e56-b16b-f30a69d21ee9",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4beaf151-7aa9-489a-971a-096fd0a689ce",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirt available: 8\n",
"Enter the quantity of mug available: -3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Quantity cannot be negative. Please enter a valid quantity: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of mug available: 7\n",
"Enter the quantity of hat available: a\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a valid quantity: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of hat available: 6\n",
"Enter the quantity of book available: 5\n",
"Enter the quantity of keychain available: 4\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d4651428-4a63-49d3-b2c0-a38ec6612c2c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(len(products))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6505b655-552c-423a-903e-0508b604e2e6",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the amount of unique products you wish to order: 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter an amount that is a non-negative, as well as, not bigger than the amount of unique products we have available\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the amount of unique products you wish to order: a\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter an numeric value: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the amount of unique products you wish to order: 3\n",
"Please enter the name of a product you wish to order: hat\n",
"Please enter the name of a product you wish to order: book\n",
"Please enter the name of a product you wish to order: basketball\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter a product that is available in our shop: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the name of a product you wish to order: keychain\n"
]
}
],
"source": [
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9ef2d90f-95e9-4707-ad10-13754c453cb6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'book', 'keychain'}\n"
]
}
],
"source": [
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "46962926-6980-4d75-be82-18af45111ab0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the price of the hat you ordered: -2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Price cannot be negative. Please enter a valid price: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the price of the hat you ordered: a\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"invalid input. Please enter a numerical value: \n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the price of the hat you ordered: 6\n",
"Please enter the price of the book you ordered: 10\n",
"Please enter the price of the keychain you ordered: 7\n"
]
}
],
"source": [
"total_to_pay = total_price(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd867742-b664-4eb4-bcbf-06e26dc1d6d6",
"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 +401,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down