Skip to content
Open

lab3 #588

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
81 changes: 79 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,88 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "71dce282",
"metadata": {},
"outputs": [],
"source": [
"\n",
"products = [\"t-shirts\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products: \n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"def get_customer_order():\n",
" customer_orders = set()\n",
" order_input = input(f\"Please enter the products you want to order separated by commas: \")\n",
" orders = order_input.split(\",\")\n",
" for order in orders:\n",
" customer_orders.add(order.strip())\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" percentage_ordered = total_ordered / len(products) * 100\n",
" return total_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_ordered, percentage_ordered = order_statistics\n",
" print(f\"total ordered: {total_ordered}\")\n",
" print(f\"percentage ordered: {percentage_ordered: .2f}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "c916022f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"total ordered: 1\n",
"percentage ordered: 20.00%\n",
"t-shirts: 5\n",
"mug: 4\n",
"hat: 5\n",
"book: 5\n",
"keychain: 5\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_order()\n",
"inventory1 = update_inventory(customer_orders, inventory)\n",
"order_stats = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_stats)\n",
"print_updated_inventory(inventory)\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +138,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down