Skip to content
Open
Show file tree
Hide file tree
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
382 changes: 382 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"source": [
"## Exercise: Managing Customer Orders with Functions\n",
"\n",
"In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"\n",
"2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n",
"\n",
"3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n",
"\n",
"5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n",
"\n",
"6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"Hints for functions:\n",
"\n",
"- Consider the input parameters required for each function and their return values.\n",
"- Utilize function parameters and return values to transfer data between functions.\n",
"- Test your functions individually to ensure they work correctly.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f21dfd36-3403-4ad1-89c2-3722479472c2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity available for t-shirt: 5\n",
"Enter the quantity available for mug: 5\n",
"Enter the quantity available for hat: 5\n",
"Enter the quantity available for book: 1\n",
"Enter the quantity available for keychain: 3\n"
]
}
],
"source": [
"#1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity available for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "83507ed3-46da-445d-8230-d0fb8efd6a9d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter a product you want to order: mug\n",
"Do you want to add more products? (Y/N): n\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'mug'}\n"
]
}
],
"source": [
"#2\n",
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
" \n",
" while True:\n",
" order = input(\"Please enter a product you want to order: \").lower()\n",
" \n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(f\"Product not found. Available products: {products}\")\n",
" continue\n",
" \n",
" while True:\n",
" question = input(\"Do you want to add more products? (Y/N): \").lower()\n",
" if question in [\"y\", \"n\"]:\n",
" break\n",
" else:\n",
" print(\"Please enter 'Y' or 'N'.\")\n",
" \n",
" if question == \"n\":\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"orders = get_customer_orders(products)\n",
"print(\"Customer orders:\", orders)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0fba3f5a-71f4-4aab-9cdc-d13b4ec949cd",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 11,
"id": "50ed8445-912f-4dfa-997c-33b74fb5abe8",
"metadata": {},
"outputs": [],
"source": [
"#3\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"{product} is out of stock and cannot be ordered.\")\n",
" else:\n",
" print(f\"{product} not found in inventory.\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "bc6dbc0f-5a65-4ed9-b41f-d708c159e04f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"({'mug'}, 20.0)\n",
"\n",
"--- Order Statistics ---\n",
"Total orders: {'mug'}\n",
"Percentage of catalog ordered: 20.00%\n"
]
}
],
"source": [
"#4\n",
"def calculate_order_statistics(orders, products):\n",
" total_order = len(orders)\n",
" percentage = (total_order / len(products)) * 100\n",
" return total_order, percentage\n",
"print(order_statistics)\n",
"#5\n",
"def print_order_statistics(order_statistics):\n",
" print(\"\\n--- Order Statistics ---\")\n",
" print(\"Total orders:\", order_statistics[0])\n",
" print(\"Percentage of catalog ordered:\", f\"{order_statistics[1]:.2f}%\")\n",
"\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "e407ff46-8a9a-4bec-b06a-bcc4e0271249",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 1, 'keychain': 3}\n",
"\n",
"--- Updated Inventory ---\n",
"t-shirt: 5\n",
"mug: 5\n",
"hat: 5\n",
"book: 1\n",
"keychain: 3\n"
]
}
],
"source": [
"#6\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\n--- Updated Inventory ---\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"print(inventory)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "fa15c2d9-064c-48cb-ab65-7c6e9421d3a9",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity available for t-shirt: 5\n",
"Enter the quantity available for mug: 4\n",
"Enter the quantity available for hat: 6\n",
"Enter the quantity available for book: 5\n",
"Enter the quantity available for keychain: 5\n",
"Please enter a product you want to order: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not found. Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter a product you want to order: hat\n",
"Do you want to add more products? (Y/N): y\n",
"Please enter a product you want to order: mug\n",
"Do you want to add more products? (Y/N): n\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Order Statistics ---\n",
"Total orders: 2\n",
"Percentage of catalog ordered: 40.00%\n",
"\n",
"--- Updated Inventory ---\n",
"t-shirt: 5\n",
"mug: 3\n",
"hat: 5\n",
"book: 5\n",
"keychain: 5\n"
]
}
],
"source": [
"#1\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity available for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"#2\n",
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"Please enter a product you want to order: \").lower()\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(f\"Product not found. Available products: {products}\")\n",
" continue\n",
" while True:\n",
" question = input(\"Do you want to add more products? (Y/N): \").lower()\n",
" if question in [\"y\", \"n\"]:\n",
" break\n",
" else:\n",
" print(\"Please enter 'Y' or 'N'.\")\n",
" if question == \"n\":\n",
" break\n",
" return customer_orders\n",
"\n",
"#3\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"{product} is out of stock and cannot be ordered.\")\n",
" else:\n",
" print(f\"{product} not found in inventory.\")\n",
" return inventory\n",
"\n",
"#4\n",
"def calculate_order_statistics(orders, products):\n",
" total_order = len(orders)\n",
" percentage = (total_order / len(products)) * 100\n",
" return total_order, percentage\n",
"\n",
"#5\n",
"def print_order_statistics(order_statistics): \n",
" print(\"\\n--- Order Statistics ---\")\n",
" print(\"Total orders:\", order_statistics[0])\n",
" print(\"Percentage of catalog ordered:\", f\"{order_statistics[1]:.2f}%\")\n",
"\n",
"#6\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\n--- Updated Inventory ---\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"#7\n",
"def main():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" inventory = initialize_inventory(products)\n",
" orders = get_customer_orders(products)\n",
" inventory = update_inventory(orders, inventory)\n",
" order_statistics = calculate_order_statistics(orders, products)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41cc8325-e2c5-4e98-8b74-31cdc8fc28f0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading