Skip to content

solved lab #455

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
126 changes: 123 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,133 @@
" - If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, display an error message and ask them to re-enter the product name. *Hint: you will need to pass inventory as a parameter*\n",
" - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n",
"\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"
"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",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd020641",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Invalid input. Please enter a whole number.\n",
"Invalid input. Please enter a whole number.\n",
"Invalid input. Please enter a whole number.\n",
"Invalid product. Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Invalid product. Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 3\n",
"mug: 2\n",
"hat: 3\n",
"book: 2\n",
"keychain: 2\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"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. Try again.\")\n",
" else:\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a whole number.\")\n",
" return inventory\n",
"\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",
" continue\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid number.\")\n",
" \n",
" customer_orders = set()\n",
" for _ in range(num_orders):\n",
" while True:\n",
" product = input(\"Enter a product the customer wants to order: \").strip().lower()\n",
" if product not in inventory:\n",
" print(f\"Invalid product. Available products: {list(inventory.keys())}\")\n",
" elif inventory[product] <= 0:\n",
" print(f\"Sorry, {product} is out of stock.\")\n",
" else:\n",
" customer_orders.add(product)\n",
" break\n",
" return customer_orders\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",
" print(\"Price cannot be negative.\")\n",
" else:\n",
" prices[product] = price\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric price.\")\n",
" return sum(prices.values())\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",
" return {k: v for k, v in inventory.items() if v > 0}\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total = len(customer_orders)\n",
" percent = (total / len(products)) * 100\n",
" return total, percent\n",
"\n",
"def print_order_statistics(stats):\n",
" total, percent = stats\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total}\")\n",
" print(f\"Percentage of Products Ordered: {percent:.2f}%\")\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",
"# ----- Main Program -----\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"print_order_statistics(calculate_order_statistics(customer_orders, products))\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print_updated_inventory(inventory)\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(f\"\\nTotal Price: {total_price:.2f}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +210,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down