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
170 changes: 169 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,174 @@
"\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": "4367d59d-9d96-409b-9750-e32d5a984911",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a606e6f9-3c4c-4b79-9c36-09fc22d6478e",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f42e4e35-729c-4e49-b643-8e661aa91638",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" while True:\n",
" try:\n",
" number_orders = int(input(\"Enter the number of customer orders: \"))\n",
"\n",
" if number_orders < 0:\n",
" raise ValueError\n",
"\n",
" break\n",
"\n",
" except ValueError:\n",
" print(\"Invalid number of orders. Please enter a valid number.\")\n",
"\n",
" customer_orders = set()\n",
"\n",
" while len(customer_orders) < number_orders:\n",
" try:\n",
" product = input(\"Enter a product you want to order: \")\n",
"\n",
" if product not in inventory:\n",
" raise ValueError\n",
"\n",
" if inventory[product] <= 0:\n",
" raise ValueError\n",
"\n",
" customer_orders.add(product)\n",
"\n",
" except ValueError:\n",
" print(\"Invalid product. Please enter a valid product with stock available.\")\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1171e0c6-1c66-4b86-be41-3c27bc1912d8",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
"\n",
" for product in customer_orders:\n",
" valid_price = False\n",
"\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
"\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
"\n",
" total_price += price\n",
" valid_price = True\n",
"\n",
" except ValueError:\n",
" print(\"Invalid price. Please enter a valid price.\")\n",
"\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec978feb-5fcc-4aa1-b684-49e93c50eaa7",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] = inventory[product] - 1\n",
"\n",
" filtered_inventory = {\n",
" product: quantity\n",
" for product, quantity in inventory.items()\n",
" if quantity > 0 \n",
" } \n",
"\n",
" return filtered_inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5177e3b1-6114-4904-8777-1c108e39227f",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage = (total_products_ordered / len(products)) * 100\n",
"\n",
" return total_products_ordered, percentage"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6408d32-4267-4018-9406-1079a70a4b05",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(total_products_ordered, percentage):\n",
" print()\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percentage}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14d7a82e-b64a-46f3-a44c-1f3fd9d58118",
"metadata": {},
"outputs": [],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"total_products_ordered, percentage = calculate_order_statistics(\n",
" customer_orders,\n",
" products\n",
")\n",
"\n",
"print_order_statistics(total_products_ordered, percentage)\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"print()\n",
"print(\"Updated Inventory:\")\n",
"\n",
"[print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n",
"\n",
"total_price = calculate_total_price(customer_orders)\n",
"\n",
"print(f\"Total Price: {total_price}\")"
]
}
],
"metadata": {
Expand All @@ -90,7 +258,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down