Skip to content

"Solved Lab" #445

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
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
135 changes: 133 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,142 @@
"\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": "7c526425",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Quantity cannot be negative. Please enter a valid quantity.\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Unique Products Ordered: 60.0\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 3\n",
"hat: 4\n",
"book: 3\n",
"keychain: 6\n",
"\n",
"Total Price: 23.0\n"
]
}
],
"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",
" print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n",
" continue\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
" return inventory\n",
"\n",
"def get_customer_orders(products, inventory):\n",
" product_map = {p.lower(): p for p in products}\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Enter 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",
" count = 0\n",
" while count < num_orders:\n",
" product_input = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n",
" if product_input in product_map:\n",
" product = product_map[product_input]\n",
" if inventory.get(product, 0) > 0:\n",
" customer_orders.add(product)\n",
" count += 1\n",
" else:\n",
" print(f\"'{product}' is out of stock.\")\n",
" else:\n",
" print(\"Invalid product name. Please try again.\")\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" # Remove items with 0 quantity\n",
" inventory = {product: qty for product, qty in inventory.items() if qty > 0}\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_ordered = (total_products_ordered / len(products)) * 100 if products else 0\n",
" return total_products_ordered, percentage_unique_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total, percentage = order_statistics\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percentage:.1f}\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" [print(f\"{product}: {qty}\") for product, qty in inventory.items()]\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",
" total_price = sum(prices.values())\n",
" return total_price\n",
"\n",
"def main():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders(products, 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:.1f}\")\n",
"\n",
"# Run the program\n",
"main()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "521e2a22",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +221,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down