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
268 changes: 266 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,275 @@
"\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": 4,
"id": "9a5ff580",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5bc92a63",
"metadata": {},
"outputs": [],
"source": [
"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. Please enter a valid quantity.\")\n",
"\n",
" inventory[product] = quantity\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" return inventory "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6e1e6966",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 4, 'hat': 3, 'book': 2, 'keychain': 1}\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7d18849b",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\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(\"The number of orders cannot be negative. Please enter a valid number.\")\n",
"\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" customer_orders = set()\n",
"\n",
" while len(customer_orders) < number_of_orders:\n",
" try:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n",
"\n",
" if product not in inventory:\n",
" raise ValueError(\"This product does not exist in the inventory. Please enter a valid product.\")\n",
"\n",
" if inventory[product] <= 0:\n",
" raise ValueError(\"This product is out of stock. Please choose another product.\")\n",
"\n",
" customer_orders.add(product)\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8cdaf00f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt', 'book', 'keychain', 'hat'}\n"
]
}
],
"source": [
"customer_orders = get_customer_orders(inventory)\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a7578e74",
"metadata": {},
"outputs": [],
"source": [
"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"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "368a410b",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_ordered = order_statistics\n",
"\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percentage_ordered:.1f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "50a94cbe",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" updated_inventory = {}\n",
"\n",
" for product, quantity in inventory.items():\n",
" updated_quantity = quantity - 1 if product in customer_orders else quantity\n",
"\n",
" if updated_quantity > 0:\n",
" updated_inventory[product] = updated_quantity\n",
"\n",
" return updated_inventory"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2b5f0b94",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = 0.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. Please enter a valid price.\")\n",
"\n",
" total_price += price\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a4996b31",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
"\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "1ccdf895",
"metadata": {},
"outputs": [],
"source": [
"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"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "51364811",
"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: 3\n",
"hat: 2\n",
"book: 2\n",
"keychain: 1\n",
"\n",
"Total Price: $15.00\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print_updated_inventory(inventory)\n",
"\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(f\"\\nTotal Price: ${total_price:.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "495c97ca",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +354,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down