Skip to content

Derya: lab functions completed #430

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
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
228 changes: 226 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,235 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "a4441357",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['mug', 'hat', 'book']\n",
"{'mug': 10, 'hat': 20, 'book': 30}\n"
]
}
],
"source": [
"product = input('Enter the name of product: ')\n",
"product_list = []\n",
"\n",
"while product: \n",
" product_list.append(product)\n",
" request = input ('Do you want to add another product: Yes / No: ')\n",
" request = request.lower()\n",
" if request == 'yes':\n",
" product = input('Enter the name of product:')\n",
" else:\n",
" break\n",
"\n",
"print(product_list)\n",
"\n",
"def initialize_inventory(products):\n",
" result = {}\n",
" for product in products:\n",
" unit = int(input('Enter the unit of' + ' ' + product))\n",
" result[product] = unit\n",
" \n",
" return result\n",
"\n",
" \n",
"\n",
"inventory_dic = initialize_inventory(product_list) \n",
"print (inventory_dic)\n"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "871f2d8d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book', 'hat'}\n"
]
}
],
"source": [
"def get_customer_orders():\n",
" result = set()\n",
" product = input('Enter the name of product: ')\n",
" while product: \n",
" result.add(product)\n",
" request = input ('Do you want to add another product: Yes / No: ')\n",
" request = request.lower()\n",
" if request == 'yes':\n",
" product = input('Enter the name of product:')\n",
" else:\n",
" break\n",
"\n",
" return (result)\n",
" \n",
"customer_orders = get_customer_orders()\n",
"print(customer_orders)\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "d073db7c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug': 10, 'hat': 18, 'book': 28}\n"
]
}
],
"source": [
"## 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",
"def update_inventory(customer_orders, inventory):\n",
" for order in customer_orders:\n",
" if order in inventory:\n",
" inventory[order] = inventory[order] - 1\n",
" \n",
" return inventory\n",
"\n",
"updated_inventory = update_inventory (customer_orders, inventory_dic)\n",
"print(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "fdc24099",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 66.66666666666666)\n"
]
}
],
"source": [
"## 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",
"def calculate_order_statistics (customer_orders, products):\n",
" total_order = len(customer_orders)\n",
" percentage = total_order / len(products) * 100\n",
" return(total_order, percentage)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, product_list)\n",
"print(order_statistics)\n",
"\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "2b03ec8d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total order: 2\n",
"Percentage 66.66666666666666\n"
]
}
],
"source": [
"##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",
"def print_order_statistics(order_statistics): \n",
" print('Total order:', order_statistics[0])\n",
" print('Percentage', order_statistics[1] )\n",
"\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "6413b752",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug 10\n",
"hat 18\n",
"book 28\n"
]
}
],
"source": [
"##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",
"def print_updated_inventory (inventory): \n",
" for name in inventory:\n",
" print(name, inventory[name])\n",
"\n",
"print_updated_inventory(updated_inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "51e905a0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug': 33, 'hat': 56, 'book': 22}\n",
"{'book', 'hat'}\n",
"{'mug': 33, 'hat': 55, 'book': 21}\n",
"(2, 66.66666666666666)\n",
"Total order: 2\n",
"Percentage 66.66666666666666\n",
"mug 33\n",
"hat 55\n",
"book 21\n"
]
}
],
"source": [
"##7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"inventory_dic = initialize_inventory(product_list) \n",
"print (inventory_dic)\n",
"customer_orders = get_customer_orders()\n",
"print(customer_orders)\n",
"updated_inventory = update_inventory (customer_orders, inventory_dic)\n",
"print(updated_inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, product_list)\n",
"print(order_statistics)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(updated_inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +285,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down