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
246 changes: 246 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
{
"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": 21,
"id": "1966607b-b4d5-4d43-a6b4-5acc4fb33c33",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is the quantity of t-shirt? 5\n",
"What is the quantity of mug? 6\n",
"What is the quantity of hat? 7\n",
"What is the quantity of book? 8\n",
"What is the quantity of keychain? 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"========== Please indicate what do you want to order ==========\n",
"Products: t-shirt, mug, hat, book, keychain\n",
"==============================================\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product name: mug\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter a product name: mug\n",
"Do you want to add another product? (yes/no): no\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" quantity = input(f\"What is the quantity of {product}? \")\n",
" inventory[product] = int(quantity)\n",
"\n",
" return inventory\n",
"inventory = initialize_inventory(products)\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = {}\n",
" user_choice2 = \"yes\"\n",
"\n",
" while user_choice2 == \"yes\":\n",
" user_choice = input(\"Enter a product that you want to order: \")\n",
"\n",
" if user_choice in products:\n",
" customer_orders[user_choice] = customer_orders.get(user_choice, 0) + 1\n",
" else:\n",
" print(\"Please enter valid product from the list.\")\n",
"\n",
" user_choice2 = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
"\n",
" return customer_orders\n",
" \n",
"customer_orders = get_customer_orders()\n",
" \n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= customer_orders[product]\n",
"\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
"\n",
" total_ordered = len(customer_orders)\n",
"\n",
" percentage_ordered = (len(customer_orders) / len(products)) * 100\n",
"\n",
" return total_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total, percentage = order_statistics\n",
"\n",
" print(\"Order Statistics:\")\n",
" print(\"======================\\n\")\n",
" print(f\"Total Products Ordered: {total}\")\n",
" print(f\"Percentage of Products Ordered: {percentage}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
"\n",
" for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "7dcfd851-5798-4450-92d9-60f447a49dbb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"========== Please indicate the stock and what do you want to order ==========\n",
"Products: t-shirt, mug, hat, book, keychain\n",
"==============================================\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is the quantity of t-shirt? 5\n",
"What is the quantity of mug? 6\n",
"What is the quantity of hat? 7\n",
"What is the quantity of book? 8\n",
"What is the quantity of keychain? 9\n",
"Enter a product name: mug\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter a product name: mug\n",
"Do you want to add another product? (yes/no): yes\n",
"Enter a product name: book\n",
"Do you want to add another product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"======================\n",
"\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0%\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 7\n",
"book: 7\n",
"keychain: 9\n"
]
}
],
"source": [
"print(\"========== Please indicate the stock and what do you want to order ==========\")\n",
"print(\"Products: t-shirt, mug, hat, book, keychain\")\n",
"print(\"==============================================\")\n",
"\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()\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9eed01ad-0111-4037-a09b-c1a2b684583c",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading