Skip to content

lab #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

lab #457

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
104 changes: 102 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,111 @@
"\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": "2d53dbe9",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
" return inventory\n",
"\n",
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"\\nEnter the number of customer orders: \"))\n",
" if num_orders < 0:\n",
" raise ValueError(\"Number of orders cannot be negative.\")\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
" for _ in range(num_orders):\n",
" while True:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n",
" if product not in inventory:\n",
" print(f\"'{product}' is not in inventory. Try again.\")\n",
" elif inventory[product] == 0:\n",
" print(f\"'{product}' is out of stock. Try again.\")\n",
" else:\n",
" customer_orders.add(product)\n",
" break\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" elif product in inventory:\n",
" print(f\"'{product}' is out of stock.\")\n",
" else:\n",
" print(f\"'{product}' is not available in the inventory.\")\n",
"\n",
" inventory = {k: v for k, v in inventory.items() if v > 0}\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" unique_ordered = len([item for item in customer_orders if item in products])\n",
" percentage_unique = (unique_ordered / len(products)) * 100\n",
" return total_ordered, percentage_unique\n",
"\n",
"def print_order_statistics(order_stats):\n",
" total_ordered, percentage_unique = order_stats\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",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" prices = {}\n",
" for product in customer_orders:\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
" prices[product] = price\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Error: {e}\")\n",
" return sum(prices.values())\n",
"\n",
"# --------- MAIN PROGRAM ---------\n",
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"order_stats = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_stats)\n",
"print_updated_inventory(inventory)\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(f\"\\nTotal Price: {total_price:.2f}\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +190,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down