Skip to content
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
90 changes: 88 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,97 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67d76bb3",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"def initialize_inventory(products):\n",
" try:\n",
" inventory = {}\n",
" for product in products:\n",
" count = int(input(f\"Enter the inventory count for {product}: \"))\n",
" if count < 0:\n",
" raise ValueError(\"Inventory count cannot be negative.\")\n",
" inventory[product] = count\n",
" return inventory\n",
" except ValueError as e:\n",
" print(f\"Input Error: {e}\")\n",
" return {}\n",
"\n",
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" order = input(f\"Please enter your Order (available products: {', '.join(products)}): \").strip().lower()\n",
" \n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(f\"Sorry, {order} is not in our product list.\")\n",
" continue\n",
"\n",
" more_product = input(\"Do you want to order more products? (yes/no): \").strip().lower()\n",
" if more_product == \"no\":\n",
" break\n",
" \n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" print(\"Processing Orders...\")\n",
" for item in customer_orders:\n",
" if inventory.get(item, 0) > 0:\n",
" inventory[item] -= 1\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" unique_products_ordered = len(customer_orders)\n",
" percentage_ordered = (unique_products_ordered / len(products)) * 100\n",
" return unique_products_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_status):\n",
" print(f\"Order Statistics:-\")\n",
" print(f\"Total Items Ordered: {order_status[0]}\")\n",
" print(f\"Percentage of Product Types Ordered: {order_status[1]:.2f}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:-\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product.capitalize()}: {quantity}\")\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"try:\n",
" inventory = initialize_inventory(products)\n",
" if not inventory:\n",
" print(\"Inventory initialization failed. Exiting program.\")\n",
" else:\n",
" customer_orders = get_customer_orders(products)\n",
" time.sleep(0.5)\n",
" if not customer_orders:\n",
" print(\"No valid orders entered.\")\n",
" else:\n",
" print(f\"Summary of items ordered: {customer_orders}\")\n",
" time.sleep(1)\n",
" total, percentage = calculate_order_statistics(customer_orders, products)\n",
" time.sleep(1)\n",
" print_order_statistics((total, percentage))\n",
" update_inventory(customer_orders, inventory)\n",
" time.sleep(1)\n",
" print_updated_inventory(inventory)\n",
"\n",
"except Exception as e:\n",
" print(f\"An unexpected error occurred: {e}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +147,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.3"
}
},
"nbformat": 4,
Expand Down