Skip to content

lab python functions done #427

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
288 changes: 288 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
{
"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": 90,
"id": "16266210-ddcb-4091-a85a-c961685c8f66",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity for t-shirt: 5\n",
"Enter the quantity for mug: 5\n",
"Enter the quantity for hat: 5\n",
"Enter the quantity for book: 5\n",
"Enter the quantity for keychain: 5\n"
]
},
{
"data": {
"text/plain": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {} \n",
"def initialize_inventory(products):\n",
"\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"initialize_inventory(products) \n",
"inventory"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "939c9cb4-cd6b-496c-8214-a18fbfd4d8bc",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Insert product you want to order hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You ordered: hat\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? yes\n",
"Insert product you want to order mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You ordered: mug\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? yes\n",
"Insert product you want to order book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You ordered: book\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your final order list: {'book', 'hat', 'mug'}\n"
]
}
],
"source": [
"customer_orders = set()\n",
"def get_customer_orders():\n",
" while True:\n",
" order = input(\"Insert product you want to order \").lower()\n",
" if order in inventory:\n",
" customer_orders.add(order)\n",
" print(f\"You ordered: {order}\")\n",
" else:\n",
" print(f\"Product {order} not found.\")\n",
" another = input(\"Do you want to add another product? \").lower()\n",
" if another != \"yes\":\n",
" break\n",
" return customer_orders\n",
" \n",
"orders = get_customer_orders()\n",
"print(\"Your final order list:\", orders)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "16ad7ac4-1002-48eb-8adb-7f8e14eeb4f9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Warning: book is out of stock!\n",
"Warning: hat is out of stock!\n",
"Warning: mug is out of stock!\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 0\n",
"hat: 0\n",
"book: 0\n",
"keychain: 5\n"
]
}
],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"Warning: {product} is out of stock!\")\n",
"update_inventory(customer_orders, inventory)\n",
"# Show updated inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"update_inventory(customer_orders, inventory)\n",
"\n",
"# Печать обновленного инвентаря\n",
"print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "b5e3742a-143c-405a-b437-238eb388d4db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 3\n",
"Percentage of Products Ordered: 30.00%\n"
]
}
],
"source": [
"def calculate_order_statistics(customer_orders,products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_products_inventory = sum(inventory.values())\n",
" \n",
" percentage_of_products_ordered = total_products_ordered/total_products_inventory*100\n",
" \n",
" return total_products_ordered, percentage_of_products_ordered\n",
"total_products_ordered, percentage_of_products_ordered = calculate_order_statistics(customer_orders, inventory)\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_of_products_ordered = order_statistics\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of Products Ordered: {percentage_of_products_ordered:.2f}%\")\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, inventory)\n",
"# Печатаем статистику заказов\n",
"print_order_statistics(order_statistics)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d43032b7-9c86-4345-8c5d-e5d706467bb4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "904c755c-db51-47e2-b69e-54c5ee085c11",
"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.13.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading