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
195 changes: 194 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,199 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "6e13d0ab-f82f-4800-bec3-5e772e063818",
"metadata": {},
"outputs": [],
"source": [
"# 1.\n",
"def initialize_inventory(products:List[str]) -> dict:\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" inventory[product] = int(input(\"Enter quantity: \"))\n",
"\n",
" return inventory\n",
"\n",
"#products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"#inventory = initialize_inventory(products)\n",
"#inventory"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "f8992e7e-7cf8-4342-b921-b2ef1c20dd0c",
"metadata": {},
"outputs": [],
"source": [
"# 2.\n",
"def get_customer_orders() -> set:\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(f\"Enter your order: \")\n",
" while order not in products:\n",
" print(\"Product not found. Try again.\")\n",
" order = input(f\"Enter your order: \")\n",
" customer_orders.add(order)\n",
" add_another = input(\"Do you want to add another product (yes/no)?\").lower()\n",
" if add_another == \"no\":\n",
" break\n",
" return customer_orders\n",
"\n",
"#customer_orders = get_customer_orders()\n",
"#customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "c58dabad-a3b9-4e1b-b296-325a3abcdcf6",
"metadata": {},
"outputs": [],
"source": [
"# 3.\n",
"def update_inventory(customer_orders: set, inventory: dict) -> dict:\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"#inventory = update_inventory(customer_orders, inventory)\n",
"#inventory"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f9850f47-e6f4-438c-8cfe-8002367cf94e",
"metadata": {},
"outputs": [],
"source": [
"# 4. \n",
"def calculate_order_statistics(customer_orders: set, products: list) -> tuple:\n",
" total_products_ordered = len(customer_orders)\n",
" percentage = (total_products_ordered / len(products)) * 100\n",
" order_statistics = (total_products_ordered, percentage) \n",
" return order_statistics\n",
"\n",
"#order_statistics = calculate_order_statistics(customer_orders, products)\n",
"#order_statistics"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "d4274a1d-c4b0-4c2d-bb02-49287b620017",
"metadata": {},
"outputs": [],
"source": [
"# 5.\n",
"def print_order_statistics(order_statistics: tuple):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: \", order_statistics[0])\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]}%\") \n",
"\n",
"#print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "21a84c90-f606-41e2-8350-1f4896901a01",
"metadata": {},
"outputs": [],
"source": [
"# 6.\n",
"def print_updated_inventory(inventory: dict):\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"#print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "5635c9f6-d216-474a-a387-72e223165533",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity: 9\n",
"Enter quantity: 8\n",
"Enter quantity: 6\n",
"Enter quantity: 9\n",
"Enter quantity: 0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 9, 'mug': 8, 'hat': 6, 'book': 9, 'keychain': 0}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter your order: book\n",
"Do you want to add another product (yes/no)? yes\n",
"Enter your order: mug\n",
"Do you want to add another product (yes/no)? yes\n",
"Enter your order: book\n",
"Do you want to add another product (yes/no)? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'mug'}\n",
"{'t-shirt': 9, 'mug': 7, 'hat': 6, 'book': 8, 'keychain': 0}\n",
"(2, 40.0)\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0%\n",
"t-shirt: 9\n",
"mug: 7\n",
"hat: 6\n",
"book: 8\n",
"keychain: 0\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = initialize_inventory(products)\n",
"print(inventory)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"print(customer_orders)\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print(inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print(order_statistics)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57087b54-db66-4015-9992-820bbd6164ff",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +254,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.0"
}
},
"nbformat": 4,
Expand Down