Skip to content

Solved lab #426

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
268 changes: 268 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
{
"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": 51,
"id": "bc0a5f4f-d7d0-4ae4-9905-cdafca5c57d4",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt : 5\n",
"mug : 5\n",
"hat : 5\n",
"book : 5\n",
"keychain : 5\n"
]
},
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"def initialize_inventory(products):\n",
" for product in products:\n",
" amount = int(input(f\"{product} : \"))\n",
" inventory[product] = amount\n",
" \n",
" return inventory\n",
"\n",
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "ce797534-374f-4434-83cc-235563d366f0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product: t-shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt added to order\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to keep on ordering? (Yes/No) yes\n",
"Enter a product: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"hat added to order\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to keep on ordering? (Yes/No) yes\n",
"Enter a product: keychain\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"keychain added to order\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to keep on ordering? (Yes/No) no\n"
]
}
],
"source": [
"def get_customer_orders():\n",
" keep_on = \"yes\"\n",
" customer_orders = set()\n",
" \n",
" while keep_on == \"yes\" : \n",
" product = input(f\"Enter a product: \").lower()\n",
" if product in inventory :\n",
" customer_orders.add(product)\n",
" print(f\"{product} added to order\")\n",
" answer = \"\"\n",
" while answer not in (\"yes\", \"no\") :\n",
" answer = input(\"Would you like to keep on ordering? (Yes/No)\").lower()\n",
" keep_on = answer \n",
" else :\n",
" print(f\"{product} not found!\")\n",
" \n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "35f5084c-793d-4ead-bd66-e490367ea7c3",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders):\n",
" for product in customer_orders :\n",
" if product in customer_orders :\n",
" inventory[product] -= 1\n",
"\n",
"update_inventory(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "c18509c4-9694-4657-ac4e-f855d75b653b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Order Statistics: Total Products Ordered: 3',\n",
" 'Percentage of Products Ordered: 13.64%']"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def calculate_order_statistics(customer_orders):\n",
" total = len(customer_orders)\n",
" percentage = round(len(customer_orders) / sum(inventory.values()) * 100, 2)\n",
" order_status = [f\"Order Statistics: Total Products Ordered: {total}\", f\"Percentage of Products Ordered: {percentage}%\"]\n",
" return order_status\n",
"\n",
"calculate_order_statistics(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "40d7e87e-ae48-47fe-9ab7-c6084295b09e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt 4\n",
"mug 5\n",
"hat 4\n",
"book 5\n",
"keychain 4\n"
]
}
],
"source": [
"def print_updated_inventory(inventory):\n",
" for key, value in inventory.items() : \n",
" print(key, value)\n",
"\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44f0a28c-3e43-4f39-8210-59327c72622a",
"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