Skip to content

solved functions lab #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
293 changes: 293 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
{
"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": 14,
"id": "16461323-ce0c-4391-bde3-aaf61da31b4d",
"metadata": {},
"outputs": [],
"source": [
"products_list = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "10f56f70-3a39-460b-80e9-249f2ba2f4d0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter amount for t-shirt: 5\n",
"Enter amount for mug: 6\n",
"Enter amount for hat: 7\n",
"Enter amount for book: 8\n",
"Enter amount for keychain: 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 6, 'hat': 7, 'book': 8, 'keychain': 9}\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" amount = int(input(f\"Enter amount for {product}: \"))\n",
" inventory[product]=amount\n",
" return inventory\n",
" \n",
"\n",
"inventory = initialize_inventory(products_list)\n",
"print(inventory) \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "445daab5-3379-4b7c-b7c0-107b675bb414",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter product or 'finish' to finish: book\n",
"Enter product or 'finish' to finish: mug\n",
"Enter product or 'finish' to finish: hat\n",
"Enter product or 'finish' to finish: finish\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"Enter product or 'finish' to finish: \")\n",
" if order.lower() == 'finish':\n",
" break \n",
" customer_orders.add(order)\n",
" return customer_orders\n",
"\n",
"orders = get_customer_orders()\n",
"print(\"Customer orders:\", orders)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "740c9275-ba6e-4d53-a3a6-f1b3a7c8f6ff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 3, 'hat': 4, 'book': 5, 'keychain': 9}\n"
]
}
],
"source": [
"# Define a function named update_inventory that takes customer_orders and inventory as parameters. \n",
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"customer_orders= {'mug', 'hat', 'book'} \n",
"\n",
"def update_inventory(customer_orders):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" \n",
" \n",
"update_inventory(customer_orders)\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "b9dee484-6c92-4612-b096-3dcfda3ee892",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 60.0)"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n",
"# Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n",
"# The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_orders, products_list):\n",
" total_order = len(customer_orders)\n",
" percentage = (total_order/len(products_list))*100\n",
" return total_order, percentage\n",
"\n",
"calculate_order_statistics(customer_orders, products_list)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "c25ee9f8-12d2-48e3-8f54-b14181f46b76",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 60.0)"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define a function named print_order_statistics that takes order_statistics as a parameter. \n",
"# Inside the function, implement the code for printing the order statistics.\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" print(f\"Total order: {total_order}\")\n",
" print(f\"Percentage: {percentage_order:.2f}%\")\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products_list)\n",
"order_statistics \n"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "b58c8f59-2fbc-4f67-a6b8-b185948e8b35",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 3, 'hat': 4, 'book': 5, 'keychain': 9}\n"
]
}
],
"source": [
"# Define a function named print_updated_inventory that takes inventory as a parameter. \n",
"# Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(inventory)\n",
" \n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6554ae01-b381-426c-98c5-f92468129160",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad4f590b-ec78-46fb-8c29-b613c1b41bfc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4d29976-8379-4330-ae8e-cf7a62e2cbec",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading