Skip to content

completed #437

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
120 changes: 118 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,127 @@
"\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": "5c60089d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: '5' is not a valid product.\n",
"Error: '2' is not a valid product.\n",
"Error: '2' is not a valid product.\n",
"Error: 't-shirts' is not a valid product.\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 5\n",
"Percentage of Unique Products Ordered: 80.0\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 3\n",
"hat: 1\n",
"book: 1\n",
"Total Price: 22.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",
" raise ValueError(\"Quantity cannot be negative.\")\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError as error:\n",
" print(f\"Error: {error}. Please enter a valid non-negative integer.\")\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",
" raise ValueError(\"Number of orders cannot be negative.\")\n",
" break\n",
" except ValueError as error:\n",
" print(f\"Error: {error}. Please enter a valid non-negative integer.\")\n",
"\n",
" orders = []\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\"Error: '{product}' is not a valid product.\")\n",
" elif inventory[product] == 0:\n",
" print(f\"Error: '{product}' is out of stock.\")\n",
" else:\n",
" orders.append(product)\n",
" break\n",
" return orders \n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total = 0.0\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",
" total += price\n",
" break\n",
" except ValueError as error:\n",
" print(f\"Error: {error}. Please enter a valid non-negative number.\")\n",
" return total\n",
"\n",
"def update_inventory(inventory, customer_orders):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
"\n",
" inventory = {product: qty for product, qty in inventory.items() if qty > 0}\n",
" return inventory\n",
"\n",
"def main():\n",
" products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders(inventory)\n",
"\n",
" # Order statistics\n",
" total_ordered = len(customer_orders)\n",
" unique_ordered = len(set(customer_orders))\n",
" percentage_unique = (unique_ordered / len(products)) * 100\n",
"\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",
" # Update inventory\n",
" inventory = update_inventory(inventory, customer_orders)\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")\n",
"\n",
" # Calculate and display total price\n",
" total_price = calculate_total_price(customer_orders)\n",
" print(f\"Total Price: {total_price:.1f}\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +206,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down