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
139 changes: 139 additions & 0 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,145 @@
"# Lab | Error Handling"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Step 1: Initialize inventory with error handling\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity available for {product}: \"))\n",
"\n",
" if quantity < 0:\n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
"\n",
" inventory[product] = quantity\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(\"Error:\", error)\n",
" print(\"Please enter a valid non-negative number.\")\n",
"\n",
" return inventory\n",
"\n",
"\n",
"# Step 2: Calculate total price with error handling\n",
"def calculate_total_price(customer_orders):\n",
" prices = {}\n",
"\n",
" for product in customer_orders:\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price for {product}: \"))\n",
"\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
"\n",
" prices[product] = price\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(\"Error:\", error)\n",
" print(\"Please enter a valid price.\")\n",
"\n",
" total_price = sum(prices.values())\n",
" return total_price\n",
"\n",
"\n",
"# Step 3: Get customer orders with error handling\n",
"def get_customer_orders(inventory):\n",
"\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
"\n",
" if num_orders <= 0:\n",
" raise ValueError(\"Number of orders must be greater than 0.\")\n",
"\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(\"Error:\", error)\n",
"\n",
" customer_orders = set()\n",
"\n",
" while len(customer_orders) < num_orders:\n",
" try:\n",
" product = input(\"Enter product name: \")\n",
"\n",
" if product not in inventory:\n",
" raise ValueError(\"Product not found in inventory.\")\n",
"\n",
" if inventory[product] <= 0:\n",
" raise ValueError(\"Product is out of stock.\")\n",
"\n",
" customer_orders.add(product)\n",
"\n",
" except ValueError as error:\n",
" print(\"Error:\", error)\n",
" print(\"Please enter a valid product.\")\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"# Update inventory after order\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
"\n",
" # remove products with 0 quantity\n",
" inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n",
"\n",
" return inventory\n",
"\n",
"\n",
"# Calculate order statistics\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"\n",
"# Print order statistics\n",
"def print_order_statistics(order_statistics):\n",
" print(\"\\nOrder Statistics\")\n",
" print(\"Total products ordered:\", order_statistics[0])\n",
" print(\"Percentage of products ordered:\", order_statistics[1], \"%\")\n",
"\n",
"\n",
"# Print updated inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory\")\n",
" for product, quantity in inventory.items():\n",
" print(product, \":\", quantity)\n",
"\n",
"\n",
"# Main Program\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(inventory)\n",
"\n",
"total_price = calculate_total_price(customer_orders)\n",
"\n",
"print(\"\\nTotal price of the customer order:\", total_price)"
]
},
{
"cell_type": "markdown",
"id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b",
Expand Down