Skip to content
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

Lab Python Functions #324

Open
wants to merge 2 commits 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
263 changes: 263 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
{
"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": 2,
"id": "3c7647ca-d446-4b17-9916-799f1f9f0fd5",
"metadata": {},
"outputs": [],
"source": [
"test_products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" # loop through all products and add quantity\n",
" for product in products:\n",
" inventory[product] = int(input(\"Please enter the quantity of \" + product + \":\"))\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9b4a0f8e-091f-4518-9b99-13c6f699ba01",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" orders = set()\n",
" answer = True\n",
" while answer == True:\n",
" orders.add(input(\"Please enter the name of your desired product: \"))\n",
" another = input(\"Do you want to add another product? Answer with 'yes' or 'no': \")\n",
" if another == \"yes\":\n",
" answer = True\n",
" elif another == \"no\":\n",
" answer = False\n",
" return orders"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "741dd52d-ab62-4925-b169-cfa824e8c3fe",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for key in customer_orders:\n",
" if key in inventory:\n",
" inventory[key] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6a27f45e-fcb4-49e0-8c1f-6b4ee418cea3",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_products_available = len(products)\n",
" percentage_ordered = total_products_ordered/total_products_available*100\n",
" return (total_products_ordered, percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "4da75305-3308-4b17-9cea-3f1c5b5bc0d5",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(\"Order statistics: \")\n",
" print(\"Total products ordered: \" + str(order_statistics[0]))\n",
" print(\"Percentage of Products ordered: \" + str(order_statistics[1]) + \"%\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f9ca7df0-2fe1-4e39-a4a7-606425ff4db7",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" for key in inventory:\n",
" print(key + \": \" + str(inventory[key]))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "b73ad429-c63a-4fd1-9388-d0589e71127c",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of t-shirt: 1\n",
"Please enter the quantity of mug: 2\n",
"Please enter the quantity of hat: 3\n",
"Please enter the quantity of book: 4\n",
"Please enter the quantity of keychain: 5\n"
]
}
],
"source": [
"test_inventory = initialize_inventory(test_products)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "d20b5f5a-2d7d-4502-8060-ff39afadb439",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the name of your desired product: mug\n",
"Do you want to add another product? Answer with 'yes' or 'no': yes\n",
"Please enter the name of your desired product: hat\n",
"Do you want to add another product? Answer with 'yes' or 'no': yes\n",
"Please enter the name of your desired product: book\n",
"Do you want to add another product? Answer with 'yes' or 'no': no\n"
]
}
],
"source": [
"test_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "b26e6e90-670a-475c-a73c-6eebedbefe4e",
"metadata": {},
"outputs": [],
"source": [
"update_inventory(test_orders, test_inventory)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "203b87ca-378a-4e2a-9fd5-d8a6f6ded180",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order statistics: \n",
"Total products ordered: 3\n",
"Percentage of Products ordered: 60.0%\n"
]
}
],
"source": [
"print_order_statistics(calculate_order_statistics(test_orders, test_products))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "3e0147e2-e2e3-4f74-b892-d656fbe79123",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: 1\n",
"mug: 1\n",
"hat: 2\n",
"book: 3\n",
"keychain: 5\n"
]
}
],
"source": [
"print_updated_inventory(test_inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5497eb81-6303-495c-9f4c-91f38a3f394a",
"metadata": {},
"outputs": [],
"source": []
}
],
"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