Skip to content
Open
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
296 changes: 295 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,300 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "74e077be-9875-4e32-853c-e6b75c50e146",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" \"\"\"\n",
" Collect avaliable amount of products in inventory dictionary\n",
"\n",
" Inputs:\n",
" -------\n",
" products: a list with the product names\n",
" inp: input data, as a number of products in stock\n",
" \"\"\"\n",
" print(\"Please, enter the avaliable quantity of each product\")\n",
" for i in products:\n",
" inp = int(input(\"{}: \".format(i)))\n",
" inventory[i] = inp"
]
},
{
"cell_type": "code",
"execution_count": 121,
"id": "9ae7b761-4eec-4b3e-9c7b-e029db8f3666",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" \"\"\"\n",
" Get the product names and collect them into 'customer_orders' set\n",
"\n",
" Outputs:\n",
" --------\n",
" customer_orders: set with product names\n",
" \"\"\"\n",
" while True:\n",
" question_1 = input(\"Do you want to add some products to your order?\").lower().strip()\n",
" \n",
" if question_1 != \"yes\":\n",
" print(\"Please, enter only 'yes' or 'no'\")\n",
" \n",
" if question_1 == \"no\":\n",
" break\n",
" \n",
" question_2 = input(\"Please, enter desired product name\").lower().strip()\n",
" \n",
" if question_2 in products:\n",
" if inventory[question_2] > 0:\n",
" customer_orders.add(question_2) \n",
" else:\n",
" print(\"Sorry, this product is out of stock\")\n",
" else:\n",
" print(\"Please, enter the name from the list\", products)\n",
"\n",
" print(\"Your order contains this products:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"id": "60ce88ff-f4a5-435c-8c56-593ea8bb6ea0",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" Update avaliable amount of products in 'inventory' dictionary usin product names from 'customer_orders' set\n",
"\n",
" Inputs:\n",
" -------\n",
" customer_orders: set of product names for order\n",
" inventory: dictionary of product names (keys) and avaliable number of items (values)\n",
" \"\"\"\n",
" for i in customer_orders:\n",
" inventory[i] = int(inventory[i]) - 1 "
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "8f9d43e8-1b25-41d6-b046-1ff596eb36a5",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"\n",
" Calculate order statistics\n",
"\n",
" Inputs:\n",
" -------\n",
" customer_orders: set of product names for order\n",
" products: list of product names \n",
"\n",
" Outputs:\n",
" --------\n",
" order_statistics: tuple of statistical parameters. 'tpo' - Total Products Ordered; 'ppo' - Percentage of Products Ordered, %\n",
" \"\"\"\n",
" tpo = len(customer_orders)\n",
" ppo = int(len(customer_orders)*100/len(products))\n",
" order_statistics = (tpo, ppo)\n",
" return order_statistics"
]
},
{
"cell_type": "code",
"execution_count": 125,
"id": "70b30e02-3e5e-4b7f-b5ec-c8c92be37629",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" \"\"\"\n",
" Prints order statistics\n",
"\n",
" Inputs:\n",
" -------\n",
" order_statistics: tuple of statistical parameters. 'tpo' - Total Products Ordered; 'ppo' - Percentage of Products Ordered, % \n",
"\n",
" Outputs:\n",
" --------\n",
" order_statistics: the data from tuple of statistical parameters\n",
" \"\"\"\n",
" print(\"Total Products Ordered: {}\".format(order_statistics[0]))\n",
" print(\"Percentage of Products Ordered: {}%\".format(order_statistics[1])) "
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "9a2bec0d-0215-4101-a5ef-4575c8e7b873",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" \"\"\"\n",
" Updates data in 'invenrory' dictionary\n",
"\n",
" Inputs:\n",
" -------\n",
" inventory: dictionary of product names (keys) and avaliable number of items (values) \n",
"\n",
" Outputs:\n",
" --------\n",
" inventory: printed data on 'inventory' dictionary\n",
" \"\"\"\n",
" print(\"The following products are availiable in the stock:\")\n",
" for p_name, i_count in inventory.items():\n",
" print(p_name + \": \" + str(i_count))"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "1cb57154-bb0b-47ed-9f42-8145498dd123",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"customer_orders = set() # Func. set() helps to identify Set instead of Dictionary -> Dictionary has no .add() method"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "4cc7bd68-0390-449e-a4a1-14d3356a39b7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please, enter the avaliable quantity of each product\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt: 3\n",
"mug: 3\n",
"hat: 3\n",
"book: 3\n",
"keychain: 3\n"
]
}
],
"source": [
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "532e8741-c96c-4dbd-9a36-cdc69577a5ec",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add some products to your order? yes\n",
"Please, enter desired product name book\n",
"Do you want to add some products to your order? yes\n",
"Please, enter desired product name mug\n",
"Do you want to add some products to your order? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please, enter only 'yes' or 'no'\n",
"Your order contains this products: {'mug', 'book'}\n"
]
}
],
"source": [
"get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "a2ce4327-0b0a-4186-b267-e5a0a18f92d1",
"metadata": {},
"outputs": [],
"source": [
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "02aae186-1268-43de-8c26-45b18fe58612",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The following products are availiable in the stock:\n",
"t-shirt: 3\n",
"mug: 2\n",
"hat: 3\n",
"book: 2\n",
"keychain: 3\n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "5c36020f-c233-4cbd-b07e-0b0ee7784105",
"metadata": {},
"outputs": [],
"source": [
"order_status = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 126,
"id": "984cdab2-c22d-4d00-a4ac-8cfc62feb312",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40%\n"
]
}
],
"source": [
"print_order_statistics(order_status)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c819ba31-d163-499a-921d-4b17b6a40365",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +355,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down