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
166 changes: 164 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,173 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c7a539b6",
"metadata": {},
"outputs": [],
"source": [
"products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "894a3f60",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "aaeca7c6",
"metadata": {},
"outputs": [],
"source": [
"#3 function to create customers\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" product_order = input(\"Enter the name of a product: \")\n",
" customer_orders.add(product_order)\n",
"\n",
" add_another = input(\"Do you want to add another product? (yes/no): \")\n",
"\n",
" if add_another.lower() == \"no\":\n",
" break\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "674e000f",
"metadata": {},
"outputs": [],
"source": [
"# 4. Function to update inventory\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"{product} is not in inventory.\")\n",
"\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "d68c9058",
"metadata": {},
"outputs": [],
"source": [
"# 5. Function to calculate statistics\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",
"\n",
" order_statistics = {\n",
" \"total_products_ordered\": total_products_ordered,\n",
" \"percentage_ordered\": percentage_ordered\n",
" }\n",
"\n",
" return order_statistics"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8c580c5c",
"metadata": {},
"outputs": [],
"source": [
"# 6. Function to print statistics\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total products ordered: {order_statistics['total_products_ordered']}\")\n",
" print(f\"Percentage of unique products ordered: {order_statistics['percentage_ordered']}%\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "700c6bbb",
"metadata": {},
"outputs": [],
"source": [
"# 7. Function to print updated inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
"\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "7a211e16",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total products ordered: 3\n",
"Percentage of unique products ordered: 60.0%\n",
"Updated Inventory:\n",
"t-shirt: 6\n",
"mug: 5\n",
"hat: 6\n",
"book: 5\n",
"keychain: 5\n"
]
}
],
"source": [
"# 8. Call the functions in the correct order\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5287ee2e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +223,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.10"
}
},
"nbformat": 4,
Expand Down