Skip to content

labka #436

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

labka #436

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
115 changes: 113 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,122 @@
"\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": "f9e12830",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Order Statistics.\n",
"Total Number of Orders: 1\n",
"Perecentage of Unique Products Ordered: 0.2%\n",
"\n",
" Updated Inventory:\n",
"t-shirt: 1\n",
"mug: 1\n",
"hat: 1\n",
"book: 1\n",
"keychain: 1\n",
"\n",
" Total Price: 1.0¢ \n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
"\n",
" inventory = {\n",
" product: get_valied_int(f\"Enter the quantity of {product}s available: \")\n",
" for product in products\n",
" }\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
" num_orders = get_valied_int(\"Enter the number of customer orders: \")\n",
"\n",
" orders = [\n",
" input(\"Enter the name of the products that the customer want orders: \")\n",
" .strip()\n",
" .lower()\n",
" for _ in range (num_orders)\n",
"\n",
" ]\n",
" return orders\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" prices = {\n",
" product: get_valied_float(f\"Enter the price of {product}: \")\n",
" \n",
" for product in customer_orders\n",
" }\n",
" total = sum(prices.values())\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",
" new_inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n",
" inventory = new_inventory\n",
" return inventory\n",
"\n",
"def get_valied_int(promt):\n",
" while True:\n",
" try:\n",
" value = int(input(promt))\n",
" if value < 0:\n",
" raise ValueError(\"The value must be a non-negative integer.\")\n",
" return value\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid integer.\")\n",
" \n",
"\n",
"def get_valied_float(promt):\n",
" while True:\n",
" try:\n",
" value = float(input(promt))\n",
" if value < 0:\n",
" raise ValueError(\"The value must be a non-negative integer.\")\n",
" return value\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid integer.\")\n",
" \n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"unique_orders = set(customer_orders)\n",
"total_ordered = len(customer_orders)\n",
"percentage_ordered = (len(unique_orders) / len(products)) * 1\n",
"\n",
"print(f\"\\n Order Statistics.\")\n",
"print(f\"Total Number of Orders: {total_ordered}\")\n",
"print(f\"Perecentage of Unique Products Ordered: {percentage_ordered:.1f}%\")\n",
"\n",
"inventory = update_inventory(inventory, customer_orders)\n",
"\n",
"print(\"\\n Updated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"total_price = calculate_total_price(unique_orders) \n",
"print(f\"\\n Total Price: {total_price}¢ \")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +201,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down