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
108 changes: 106 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,115 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "776bba80",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'keychain', 'book'}\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 10.0%\n",
"Updated inventory:\n",
"t-shirt: 4\n",
"mug: 4\n",
"hat: 4\n",
"book: 3\n",
"keychain: 3\n"
]
}
],
"source": [
"# Let's readjust the code to the new requirements. \n",
"\n",
"\n",
"\n",
"# Defining all the functions: \n",
"# inventory initialization function\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"# customer orders function\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" n = 0\n",
" while n >= 0:\n",
" order = input(f\"Enter the name of a product you would like to order {n+1}: \")\n",
" if order in products and inventory[order] > 0:\n",
" customer_orders.add(order)\n",
" n+=1 \n",
" else:\n",
" print(f\"{order} is not available in the products list.\\nWe only sell t-shirts, mugs, hats, books, keychains\")\n",
" order_more = input(\"Would you like to order more products? \\n type Y for \\\"yes\\\", N for \\\"no\\\": \").strip().lower()\n",
" while order_more not in [\"y\", \"n\"]:\n",
" print(\"Invalid input. Please type Y for \\\"yes\\\" or N for \\\"no\\\".\")\n",
" order_more = input(\"Would you like to order more products? \\n type Y for \\\"yes\\\", N for \\\"no\\\": \").strip().lower()\n",
" continue\n",
" if order_more == \"Y\" or order_more == \"y\":\n",
" continue\n",
" else:\n",
" break\n",
" return customer_orders \n",
"\n",
"# order statistics function\n",
"def calculate_order_statistics(customer_orders, inventory):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_products_ordered = (total_products_ordered / sum(inventory.values())) * 100\n",
" return total_products_ordered, percentage_products_ordered\n",
"\n",
"# print order statistics function\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_products_ordered = order_statistics\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of Products Ordered: {percentage_products_ordered}%\")\n",
" return None\n",
"# inventory update function\n",
"def print_updated_inventory(customer_orders, inventory):\n",
" print(\"Updated inventory:\")\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" elif product in inventory and inventory[product] == 0:\n",
" print(f\"{product} is out of stock.\")\n",
" for p in inventory:\n",
" print(f\"{p}: {inventory[p]}\")\n",
" return None\n",
"\n",
"#Define the list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"print(\"Customer orders:\", customer_orders)\n",
"\n",
"# The statistic we needed: \n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, inventory)\n",
"print_order_statistics(order_statistics)\n",
"\n",
"\n",
"print_updated_inventory(customer_orders, inventory)\n",
"\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +165,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.7"
}
},
"nbformat": 4,
Expand Down