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
138 changes: 137 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,142 @@
"\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": "c91de722-db15-44e1-9bc5-b85b1fad9d24",
"metadata": {},
"outputs": [],
"source": [
"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 whole number.\")\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders(inventory):\n",
" # Get valid number of orders\n",
" valid_num = False\n",
" while not valid_num:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders >= 0:\n",
" valid_num = True\n",
" else:\n",
" print(\"Number of orders cannot be negative.\")\n",
" except ValueError:\n",
" print(\"Invalid input! Please enter a whole number.\")\n",
"\n",
" customer_orders = set()\n",
"\n",
" # Get valid product names\n",
" for _ in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \").lower().strip()\n",
" \n",
" if product not in inventory:\n",
" print(f\"Error: '{product}' is not in the inventory. Please enter a valid product.\")\n",
" elif inventory[product] <= 0:\n",
" print(f\"Error: '{product}' is out of stock. Please select another product.\")\n",
" else:\n",
" customer_orders.add(product)\n",
" valid_product = True\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" percentage_unique = (total_ordered / len(products)) * 100\n",
" return total_ordered, percentage_unique\n",
"\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_ordered, percentage_unique = order_statistics\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total_ordered}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percentage_unique:.1f}%\")\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" # Step 1: Subtract 1 for ordered items\n",
" updated = {\n",
" product: quantity - 1 if product in customer_orders else quantity\n",
" for product, quantity in inventory.items()\n",
" }\n",
" # Step 2: Keep only items with stock remaining\n",
" return {\n",
" product: quantity \n",
" for product, quantity in updated.items() \n",
" if quantity > 0\n",
" }\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total_price = 0.0\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",
" total_price += price\n",
" valid_price = True\n",
" else:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value.\")\n",
" return total_price\n",
"\n",
"\n",
"# Main Program Execution\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 1. Initialize inventory with input validation\n",
"inventory = initialize_inventory(products)\n",
"\n",
"# 2. Get customer orders with validation against inventory & stock\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"# 3. Calculate and display order statistics\n",
"order_stats = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_stats)\n",
"\n",
"# 4. Update and display inventory\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print_updated_inventory(inventory)\n",
"\n",
"# 5. Calculate and display total order price with input validation\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(f\"\\nTotal Price: {total_price:.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bca185b3-a844-4816-9a9c-0aacb30b256a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -90,7 +226,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down