Skip to content

Lab Solved #419

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
176 changes: 175 additions & 1 deletion lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,180 @@
"\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": 14,
"id": "bede8213-7129-4407-9f96-4f21a7f21b36",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: -5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Quantity cannot be negative. Please enter a valid quantity.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 8\n",
"Enter the quantity of hats available: 10\n",
"Enter the quantity of books available: 4\n",
"Enter the quantity of keychains available: 6\n",
"Enter the number of customer orders: 3\n",
"Enter the name of a product for order 1: 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid product name. Please choose from the available inventory.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product for order 1: hat\n",
"Enter the name of a product for order 2: t-shirt\n",
"Enter the name of a product for order 3: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Unique Products Ordered: 60.0%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 4\n",
"mug: 7\n",
"hat: 9\n",
"book: 4\n",
"keychain: 6\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of mug: 5\n",
"Enter the price of hat: 15\n",
"Enter the price of t-shirt: 30\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Total Price: $50.00\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",
" else:\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value.\")\n",
" return inventory \n",
"def get_customer_orders(inventory):\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders < 0:\n",
" print(\"Number of orders cannot be negative.\")\n",
" else:\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
"\n",
" orders = []\n",
" for i in range(num_orders):\n",
" while True:\n",
" product = input(f\"Enter the name of a product for order {i + 1}: \").strip().lower()\n",
" if product not in inventory:\n",
" print(\"Invalid product name. Please choose from the available inventory.\")\n",
" elif inventory[product] == 0:\n",
" print(f\"Sorry, {product}s are out of stock.\")\n",
" else:\n",
" orders.append(product)\n",
" break\n",
" return orders\n",
"\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" prices = {}\n",
" for product in set(customer_orders):\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
" if price < 0:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" else:\n",
" prices[product] = price\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value.\")\n",
" return sum(prices[product] for product in customer_orders)\n",
"\n",
"\n",
"def update_inventory(inventory, customer_orders):\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" return {product: qty for product, qty in inventory.items() if qty > 0}\n",
"\n",
"\n",
"def main():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" inventory = initialize_inventory(products)\n",
"\n",
" customer_orders = get_customer_orders(inventory)\n",
"\n",
" print()\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {len(customer_orders)}\")\n",
" percent_unique = (len(set(customer_orders)) / len(products)) * 100\n",
" print(f\"Percentage of Unique Products Ordered: {percent_unique:.1f}%\")\n",
"\n",
" inventory = update_inventory(inventory, customer_orders)\n",
"\n",
" print()\n",
" print(\"Updated Inventory:\")\n",
" for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")\n",
"\n",
" total_price = calculate_total_price(customer_orders)\n",
" print(f\"\\nTotal Price: ${total_price:.2f}\")\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
]
}
],
"metadata": {
Expand All @@ -90,7 +264,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down