Skip to content

lab 4 #455

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 2 commits into
base: main
Choose a base branch
from
Open

lab 4 #455

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
342 changes: 342 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
{
"cells": [
{
"cell_type": "raw",
"id": "67c29ba6-5fe5-4d4a-8eb2-fc62adbfc9e8",
"metadata": {},
"source": [
"# Lab | Functions"
]
},
{
"cell_type": "raw",
"id": "3a984506-2423-4320-939b-88a1b048d220",
"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": "e60a44f1-5b97-4e0c-923e-504790a7bf25",
"metadata": {},
"outputs": [],
"source": [
"#1.\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"def initialize_inventory(products):\n",
" global inventory\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"please input the quantity of {product}\"))\n",
" inventory[product]= quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "27908571-eb2b-46a1-884b-270f1f6d2808",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"please input the quantity of t-shirt 55\n",
"please input the quantity of mug 244\n",
"please input the quantity of hat 25\n",
"please input the quantity of book 78\n",
"please input the quantity of keychain 33\n"
]
},
{
"data": {
"text/plain": [
"{'t-shirt': 55, 'mug': 244, 'hat': 25, 'book': 78, 'keychain': 33}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "46dd1902-7747-46c1-8899-42bd65f02b19",
"metadata": {},
"outputs": [],
"source": [
"#2 \n",
"customer_orders = set()\n",
"def get_customer_orders():\n",
" answer=input(\"do you want to add another product? yes or no\")\n",
" while answer==\"yes\":\n",
" order=input(\"product the customer wants to order\")\n",
" answer=input(\"do you want to add another product? yes or no\")\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "bdf34740-9b75-42af-929c-3125f7a01026",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"do you want to add another product? yes or no yes\n",
"product the customer wants to order hat\n",
"do you want to add another product? yes or no yes\n",
"product the customer wants to order mug\n",
"do you want to add another product? yes or no no\n"
]
},
{
"data": {
"text/plain": [
"{'hat', 'mug'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b4d19da9-2802-4722-8d03-18fc92e1a053",
"metadata": {},
"outputs": [],
"source": [
"#3\n",
"def update_inventory(customer_orders, inventory):\n",
" for products in inventory:\n",
" if products in customer_orders:\n",
" inventory[products] = inventory[products]-1\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "c1e86ca1-8692-4ffe-89fc-81d0fb1b4cea",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 55, 'mug': 241, 'hat': 22, 'book': 78, 'keychain': 33}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "03b7bdfd-2bbb-4284-b547-528d4e565ff4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 55, 'mug': 242, 'hat': 23, 'book': 78, 'keychain': 33}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "b938ea5c-e5cc-40b6-91a2-d6f085bccc16",
"metadata": {},
"outputs": [],
"source": [
"#4\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_ordered = total_products_ordered / sum(inventory.values())\n",
" return total_products_ordered, percentage_unique_ordered"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "c7d7c58d-8dbb-4f69-9d7d-71c2e62fe714",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 0.004662004662004662)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "f5459923-9357-47a0-96cc-abff937a5741",
"metadata": {},
"outputs": [],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "480ea4df-bb6c-4c2a-9b1e-357e37954353",
"metadata": {},
"outputs": [],
"source": [
"#5\n",
"def print_order_statistics(order_statistics):\n",
" print (calculate_order_statistics(customer_orders, products))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "05fc13d3-a3ec-41c1-8b20-8a141879384b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 0.004662004662004662)\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "8ae3257d-f6d9-45a3-94d1-436a89993395",
"metadata": {},
"outputs": [],
"source": [
"#5\n",
"def print_updated_inventory(inventory):\n",
" print(update_inventory(customer_orders, inventory))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "030b91bd-7fc9-42d5-9bae-85c248e7006c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 55, 'mug': 240, 'hat': 21, 'book': 78, 'keychain': 33}\n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b143c5ad-4314-4919-a370-5ed7c3081e3f",
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading