Skip to content

Solved Lab #434

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
276 changes: 276 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"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": 98,
"id": "3634d44d-d52e-4256-8337-d8bd091193ec",
"metadata": {},
"outputs": [],
"source": [
"#1\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" value = int(input(\"What is the amount of \"+product+\"?\"))\n",
" inventory[product] = value\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "7921fac7-eaca-4532-a515-6a5d47c26c3c",
"metadata": {},
"outputs": [],
"source": [
"#2 \n",
"def get_customer_orders():\n",
" print(\"Here is the current list of products: \")\n",
" print(products)\n",
" \n",
" customer_orders = set()\n",
" \n",
" check = True\n",
" product = input(\"Add a product: \")\n",
"\n",
" while check == True:\n",
" customer_orders.add(product)\n",
" question = input(\"Do you want to adda another product (yes/no)?\")\n",
" if question == \"yes\":\n",
" product = input(\"Add a product: \")\n",
" elif question == \"no\":\n",
" check = False\n",
" \n",
" return(customer_orders)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "d73ad01e-ce54-4c8f-8f09-ba2524fc4704",
"metadata": {},
"outputs": [],
"source": [
"#3\n",
"def update_inventory(customer_orders, inventory):\n",
" for i in customer_orders:\n",
" inventory[i] = inventory[i] - 1\n",
" return(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "e1253f89-38fe-4272-b6e2-e9b9fba62566",
"metadata": {},
"outputs": [],
"source": [
"#4\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
"\n",
" total_products = sum(inventory.values())\n",
" \n",
" percentage_products_ordered = (total_products_ordered / total_products)*100\n",
"\n",
" order_statistics = (total_products_ordered, percentage_products_ordered)\n",
" \n",
" return(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 106,
"id": "02d69b7c-1718-4d62-bae8-659b58f24153",
"metadata": {},
"outputs": [],
"source": [
"#5\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics: \")\n",
" print(\"Total Products Ordered: \"+str(order_statistics[0]))\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "2ac744e2-b5ed-4e8d-a309-c96ada7b5df2",
"metadata": {},
"outputs": [],
"source": [
"#6\n",
"def print_updated_inventory(inventory):\n",
" print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "52612451-7691-408b-8324-c82f6765093b",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"What is the amount of tshirt? 3\n",
"What is the amount of mug? 4\n",
"What is the amount of hat? 5\n",
"What is the amount of book? 6\n",
"What is the amount of keychain? 3\n"
]
}
],
"source": [
"#7\n",
"products = [\"tshirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "5eb176e1-2674-44cc-9c85-52640ae18f0b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here is the current list of products: \n",
"['tshirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add a product: tshirt\n",
"Do you want to adda another product (yes/no)? yes\n",
"Add a product: hat\n",
"Do you want to adda another product (yes/no)? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'hat', 'tshirt'}\n"
]
}
],
"source": [
"customer_orders = get_customer_orders()\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "6c4cb66d-bab8-48da-88cf-71393bc7458a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'tshirt': 3, 'mug': 4, 'hat': 4, 'book': 6, 'keychain': 3}\n"
]
}
],
"source": [
"inventory = update_inventory(customer_orders, inventory)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"id": "1fa7a571-316d-4c5e-a834-aa2c85404ffa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics: \n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 10.00%\n"
]
}
],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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