Skip to content
Open
Show file tree
Hide file tree
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
237 changes: 237 additions & 0 deletions .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Error Handling"
]
},
{
"cell_type": "markdown",
"id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b",
"metadata": {},
"source": [
"## Exercise: Error Handling for Managing Customer Orders\n",
"\n",
"The implementation of your code for managing customer orders assumes that the user will always enter a valid input. \n",
"\n",
"For example, we could modify the `initialize_inventory` function to include error handling.\n",
" - If the user enters an invalid quantity (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the quantity for that product.\n",
" - Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n",
"\n",
"```python\n",
"# Step 1: Define the function for initializing the inventory with error handling\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_quantity = False\n",
" while not valid_quantity:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n",
" valid_quantity = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"# Or, in another way:\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}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\n",
"```\n",
"\n",
"Let's enhance your code by implementing error handling to handle invalid inputs.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"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.\n",
"\n",
"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",
"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": 27,
"id": "df2c29ad-43e5-42b2-b0d3-bd3282db464d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the prices of hat e\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please put absolute numbers! \n",
"None\n"
]
}
],
"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), \n",
"#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.\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
"\n",
" for product, quantity in customer_orders.items():\n",
" try : \n",
" prices_input = input(f\"Enter the prices of {product}\")\n",
" prices_list = prices_input.split(\",\")\n",
" prices = [int(price.strip()) for price in prices_list if price] #list comprehension\n",
" \n",
" except ValueError :\n",
" print (\"Please put absolute numbers! \")\n",
" return None\n",
" \n",
" product_total_price = sum(prices) * quantity\n",
" total_price += product_total_price\n",
" \n",
" return total_price\n",
" \n",
"customer_orders = {\"hat\":1, \"mug\":2, \"book\":5, \"t-shirt\":3, \"keychain\":2}\n",
"print (calculate_total_price (customer_orders))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "cf3944d3-85e8-4bfe-bbe8-c84d8c68226a",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many product are you ordering? -1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You cannot enter negative number!\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many product are you ordering? e\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"please re-enter a valid absolute number!\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many product are you ordering? 1\n",
"Enter the name of product: hat\n",
"Enter the quantity: 1\n"
]
},
{
"data": {
"text/plain": [
"{'hat': 1}"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"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), \n",
"#display an error message and ask them to re-enter the number of orders\n",
"#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",
"def get_customer_orders(inventory) : \n",
" while True :\n",
" try :\n",
" num_of_products = int (input (\"How many product are you ordering?\"))\n",
" if num_of_products < 0 :\n",
" print (\"You cannot enter negative number!\")\n",
" else :\n",
" break \n",
" except ValueError : \n",
" print (\"please re-enter a valid absolute number!\")\n",
" \n",
" customer_orders = {input(\"Enter the name of product: \"): int(input(\"Enter the quantity: \")) for _ in range(num_of_products)}\n",
" return customer_orders\n",
"\n",
"inventory = {\"hat\":1, \"mug\":2, \"book\":5, \"t-shirt\":3, \"keychain\":2}\n",
"get_customer_orders (inventory)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "7e59ff37-bebe-4562-b72d-03849bcc3e4c",
"metadata": {},
"outputs": [],
"source": [
"#4 Test your code by running the program and deliberately entering invalid quantities and product names. \n",
"#Make sure the error handling mechanism works as expected."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
122 changes: 0 additions & 122 deletions README.md

This file was deleted.

Loading