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
112 changes: 110 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,119 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "664593e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Products in customer orders: {'keychain', 'book', 'mug', 'hat'}\n",
"t-shirt, Quantity Left: 5\n",
"mug, Quantity Left: 5\n",
"hat, Quantity Left: 6\n",
"book, Quantity Left: 5\n",
"keychain, Quantity Left: 4\n",
"Order Statistics\n",
"Total Products Ordered: 4\n",
"Percentage of Products Ordered: 80.0 %\n"
]
}
],
"source": [
"#1 \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"#2\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for item in products:\n",
" quantity = int(input(f\"Enter the number of {item} available: \"))\n",
" inventory[item] = quantity\n",
" return inventory\n",
"#3\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" product = input(\"Enter the product name: \").lower()\n",
" \n",
" if product in products:\n",
" customer_orders.add(product) \n",
" else:\n",
" print(\"Invalid product. Please choose from:\", products)\n",
" \n",
" while True:\n",
" add_more = input(\"Do you want to order another product? (yes/no): \").lower()\n",
" if add_more in (\"yes\", \"no\"):\n",
" break\n",
" print(\"Please answer in 'yes' or 'no'.\")\n",
" if add_more == \"no\":\n",
" break\n",
" print(\"Products in customer orders:\", customer_orders)\n",
" return customer_orders\n",
"\n",
"\n",
"#4\n",
"def update_inventory(inventory,customer_orders):\n",
" for product_name in customer_orders:\n",
" if product_name in inventory:\n",
" inventory[product_name] -= 1\n",
" for item in products:\n",
" print(f\"{item}, Quantity Left: {inventory[item]}\") \n",
" return inventory\n",
"\n",
"#5\n",
"def calculate_order_statistics(customer_orders,products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" order_status = (total_products_ordered, percentage_ordered)\n",
" return order_status\n",
"#6\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics\")\n",
" print(\"Total Products Ordered:\", order_statistics[0])\n",
" print(\"Percentage of Products Ordered:\", order_statistics[1], \"%\")\n",
"#7\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for item, qty in inventory.items():\n",
" print(f\"{item}: {qty}\")\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"inventory = update_inventory(inventory,customer_orders)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders,products)\n",
"\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2271b00",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "80b89dcf",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +169,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down