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
157 changes: 155 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,164 @@
"\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": 26,
"id": "3568d015",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" while True:\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",
" inventory[product] = quantity\n",
" break\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7456c646",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: doll is not available.\n",
"{'mug', 'book'}\n"
]
}
],
"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), \n",
" # or that doesn't have stock available, display an error message and ask them to re-enter the product name. \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",
"\n",
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" try:\n",
" order_number = int(input(\"Enter the number of orders: \"))\n",
" if order_number <= 0:\n",
" raise ValueError(\"Invalid number of orders! Please enter a non-negative value.\")\n",
" break\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" for _ in range(order_number):\n",
"\n",
" while True:\n",
" try:\n",
" product = input(\"Enter product name: \")\n",
" if product not in inventory:\n",
" raise ValueError(f\"Product {product} is not available.\")\n",
" if inventory[product] <= 0:\n",
" raise ValueError(\"Product is out of stock\")\n",
" \n",
" customer_orders.add(product)\n",
" break \n",
" \n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" \n",
" \n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders(inventory)\n",
"print(customer_orders)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "052c06f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 3, 'mug': 19, 'hat': 22, 'book': 32, 'keychain': 12}\n"
]
}
],
"source": [
"#3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. \n",
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory \n",
"\n",
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"print(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "49c3b175",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"34.0\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 total_price_of_order(customer_orders):\n",
" product_price = {} \n",
"\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",
" product_price[product] = price \n",
" valid_price = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" total_price = sum(product_price.values())\n",
" return total_price\n",
"\n",
"total_price_of_order = total_price_of_order(customer_orders)\n",
"print(total_price_of_order)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +243,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down