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
136 changes: 134 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,138 @@
"# Lab | Functions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "252b5113",
"metadata": {},
"outputs": [],
"source": [
"# Function 1: Initialize inventory\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter quantity available for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "233e98fa",
"metadata": {},
"outputs": [],
"source": [
"# Function 2: Get customer orders\n",
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
" while True:\n",
" product = input(\"Enter a product to order: \").strip().lower()\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" else:\n",
" print(\"Invalid product. Skipped.\")\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another != \"yes\":\n",
" break\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c6326956",
"metadata": {},
"outputs": [],
"source": [
"# Function 3: Update inventory\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" if item in inventory and inventory[item] > 0:\n",
" inventory[item] -= 1\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "92256644",
"metadata": {},
"outputs": [],
"source": [
"# Function 4: Calculate order statistics\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" percent_ordered = (total_ordered / len(products)) * 100\n",
" return (total_ordered, f\"{percent_ordered:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7b8f7e9f",
"metadata": {},
"outputs": [],
"source": [
"# Function 5: Print order statistics\n",
"def print_order_statistics(order_status):\n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", order_status[0])\n",
" print(\"Percentage of Products Ordered:\", order_status[1])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "ca57dac4",
"metadata": {},
"outputs": [],
"source": [
"# Function 6: Print updated inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "42a93596",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.00%\n",
"Updated Inventory:\n",
"t-shirt: 9\n",
"mug: 7\n",
"hat: 4\n",
"book: 6\n",
"keychain: 4\n"
]
}
],
"source": [
"# Main function to run all steps\n",
"def main():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders(products)\n",
" inventory = update_inventory(customer_orders, inventory)\n",
" order_status = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_status)\n",
" print_updated_inventory(inventory)\n",
"\n",
"# Run the full program\n",
"main()"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
Expand Down Expand Up @@ -47,7 +179,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +193,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down