Skip to content
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
Binary file added .DS_Store
Binary file not shown.
216 changes: 216 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
{
"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": 7,
"id": "400c3bcf-2fd2-45d2-8ad2-0aa14512c768",
"metadata": {},
"outputs": [],
"source": [
"# Function 1\n",
"def initialize_inventory (products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity =int(input(f\"Enter the quantity available for {product}: \"))\n",
" inventory [product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "4cd2aa34-a562-4e3d-880a-5a6424dc3ff1",
"metadata": {},
"outputs": [],
"source": [
"# Function 2\n",
"def get_customer_orders ():\n",
" customer_orders = set()\n",
" while True:\n",
" order=input(\"Enter the other product that customer wants: \")\n",
" customer_orders.add(order)\n",
" another_product=input(\"Would you like to add another product?(Answer only Yes or No) \")\n",
"\n",
" if another_product==\"No\" or another_product==\"no\":\n",
" break\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "0b37d811-1c58-43fa-8a5d-0e20d69216c4",
"metadata": {},
"outputs": [],
"source": [
"# Function 3\n",
"def update_inventory (customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -=1 \n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4ccb2958-bb2e-4a65-a056-62275e028438",
"metadata": {},
"outputs": [],
"source": [
"# Function 4\n",
"def calculate_order_statistics (customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" \n",
" percentage_ordered = (total_products_ordered / len(products)) * 10\n",
" \n",
" return total_products_ordered,percentage_ordered\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "feb44e20-9522-4456-a6c6-40e5ad531c5c",
"metadata": {},
"outputs": [],
"source": [
"#Function 5\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered = order_statistics [0]\n",
" percentage_ordered = order_statistics [1]\n",
" \n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", total_products_ordered)\n",
" print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "06355cb6-c24b-4a3f-9621-5532a6a63657",
"metadata": {},
"outputs": [],
"source": [
"#Function 6\n",
"def print_updated_inventory (inventory):\n",
" print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "aa846742-a705-41ce-9b4a-2292ff66858c",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity available for t-shirt: 5\n",
"Enter the quantity available for mug: 5\n",
"Enter the quantity available for hat: 5\n",
"Enter the quantity available for book: 5\n",
"Enter the quantity available for keychain: 5\n",
"Enter the other product that customer wants: hat\n",
"Would you like to add another product?(Answer only Yes or No) yes\n",
"Enter the other product that customer wants: book\n",
"Would you like to add another product?(Answer only Yes or No) yes\n",
"Enter the other product that customer wants: mug\n",
"Would you like to add another product?(Answer only Yes or No) yes\n",
"Enter the other product that customer wants: t-shirt\n",
"Would you like to add another product?(Answer only Yes or No) yes\n",
"Enter the other product that customer wants: keychain\n",
"Would you like to add another product?(Answer only Yes or No) no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 5\n",
"Percentage of Products Ordered: 10.0 %\n",
"{'t-shirt': 4, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 4}\n"
]
}
],
"source": [
"#Step 7\n",
"\n",
"# List of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory =initialize_inventory(products)\n",
"customer_orders = get_customer_orders ()\n",
"update_inventory (customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics (customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory (inventory)\n"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
153 changes: 150 additions & 3 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,160 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "400c3bcf-2fd2-45d2-8ad2-0aa14512c768",
"metadata": {},
"outputs": [],
"source": [
"# Function 1\n",
"def initialize_inventory (products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity =int(input(f\"Enter the quantity available for {product}: \"))\n",
" inventory [product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "4cd2aa34-a562-4e3d-880a-5a6424dc3ff1",
"metadata": {},
"outputs": [],
"source": [
"# Function 2\n",
"def get_customer_orders ():\n",
" customer_orders = set()\n",
" while True:\n",
" order=input(\"Enter the other product that customer wants: \")\n",
" customer_orders.add(order)\n",
" another_product=input(\"Would you like to add another product?(Answer only Yes or No) \")\n",
"\n",
" if another_product==\"No\" or another_product==\"no\":\n",
" break\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "0b37d811-1c58-43fa-8a5d-0e20d69216c4",
"metadata": {},
"outputs": [],
"source": [
"# Function 3\n",
"def update_inventory (customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -=1 \n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4ccb2958-bb2e-4a65-a056-62275e028438",
"metadata": {},
"outputs": [],
"source": [
"# Function 4\n",
"def calculate_order_statistics (customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" \n",
" percentage_ordered = (total_products_ordered / len(products)) * 10\n",
" \n",
" return total_products_ordered,percentage_ordered\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "feb44e20-9522-4456-a6c6-40e5ad531c5c",
"metadata": {},
"outputs": [],
"source": [
"#Function 5\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered = order_statistics [0]\n",
" percentage_ordered = order_statistics [1]\n",
" \n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", total_products_ordered)\n",
" print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "06355cb6-c24b-4a3f-9621-5532a6a63657",
"metadata": {},
"outputs": [],
"source": [
"#Function 6\n",
"def print_updated_inventory (inventory):\n",
" print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "aa846742-a705-41ce-9b4a-2292ff66858c",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity available for t-shirt: 5\n",
"Enter the quantity available for mug: 5\n",
"Enter the quantity available for hat: 5\n",
"Enter the quantity available for book: 5\n",
"Enter the quantity available for keychain: 5\n",
"Enter the other product that customer wants: hat\n",
"Would you like to add another product?(Answer only Yes or No) yes\n",
"Enter the other product that customer wants: book\n",
"Would you like to add another product?(Answer only Yes or No) yes\n",
"Enter the other product that customer wants: mug\n",
"Would you like to add another product?(Answer only Yes or No) yes\n",
"Enter the other product that customer wants: t-shirt\n",
"Would you like to add another product?(Answer only Yes or No) yes\n",
"Enter the other product that customer wants: keychain\n",
"Would you like to add another product?(Answer only Yes or No) no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 5\n",
"Percentage of Products Ordered: 10.0 %\n",
"{'t-shirt': 4, 'mug': 4, 'hat': 4, 'book': 4, 'keychain': 4}\n"
]
}
],
"source": [
"#Step 7\n",
"\n",
"# List of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory =initialize_inventory(products)\n",
"customer_orders = get_customer_orders ()\n",
"update_inventory (customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics (customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory (inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -61,7 +208,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down