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
159 changes: 157 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,166 @@
"\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": 1,
"id": "565e0de5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Unique Products Ordered: 40.0\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 2\n",
"book: 2\n",
"Total Price: 15.0\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\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 of {product}s available: \"))\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(f\"Invalid quantity: {error}\")\n",
"\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" try:\n",
" number_of_orders = int(input(\"Enter the number of customer orders: \"))\n",
"\n",
" if number_of_orders < 0:\n",
" raise ValueError(\"Number of orders cannot be negative.\")\n",
"\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Invalid number of orders: {error}\")\n",
"\n",
" for _ in range(number_of_orders):\n",
" while True:\n",
" try:\n",
" order = input(\n",
" \"Enter the name of a product that a customer wants to order: \"\n",
" )\n",
"\n",
" if order not in inventory:\n",
" raise ValueError(\"Product does not exist.\")\n",
"\n",
" if inventory[order] <= 0:\n",
" raise ValueError(\"Product is out of stock.\")\n",
"\n",
" customer_orders.add(order)\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Invalid product: {error}\")\n",
"\n",
" return customer_orders\n",
"\n",
"\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",
"\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"\n",
"def update_inventory(inventory, customer_orders):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
"\n",
" inventory = {\n",
" product: quantity\n",
" for product, quantity in inventory.items()\n",
" if quantity > 0\n",
" }\n",
"\n",
" return inventory\n",
"\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
"\n",
" for product in customer_orders:\n",
" while True:\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",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Invalid price: {error}\")\n",
"\n",
" return total_price\n",
"\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of Unique Products Ordered: {order_statistics[1]}\")\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
"\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"inventory = update_inventory(inventory, customer_orders)\n",
"\n",
"total_price = calculate_total_price(customer_orders)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(inventory)\n",
"\n",
"print(f\"Total Price: {total_price}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +245,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down