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
269 changes: 259 additions & 10 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,278 @@
"\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",
"Follow the steps below to complete the exercise:"
]
},
{
"cell_type": "markdown",
"id": "5651c944",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "460c68f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"inventory updated:\n",
"{'t-shirt': 6, 'mug': 7, 'hat': 8, 'book': 3, 'keychain': 5}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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",
"def initialize_inventory (products):\n",
" inventory = {}\n",
" for item in products:\n",
" quantity = int(input(f\"how many {item} I have ?\"))\n",
" inventory [item] = quantity\n",
" return (inventory)\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",
"inventory = initialize_inventory (products)\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",
"print (\"inventory updated:\")\n",
"print (inventory)"
]
},
{
"cell_type": "markdown",
"id": "fc6003cc",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "bc8b40a6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug added to your order\n",
"iguana is not a product available\n",
"book added to your order\n",
"hat added to your order\n",
"keychain added to your order\n",
"products ordered\n",
"{'book', 'keychain', 'mug', 'hat'}\n"
]
}
],
"source": [
"def get_customer_orders ():\n",
" customer_orders = set()\n",
" while True:\n",
" item = input(\"What product you want to order (t-shirt, mug, hat, book, keychain)? \")\n",
" if item in products:\n",
" customer_orders.add(item)\n",
" print (f'{item}', \"added to your order\")\n",
" else:\n",
" print (f'{item}' , \"is not a product available\")\n",
" more_items = input (\"want to add another product (yes/no)\")\n",
" if more_items != \"yes\":\n",
" break\n",
" return (customer_orders) \n",
" \n",
"customer_orders = get_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",
"print (\"products ordered\")\n",
"print (customer_orders)"
]
},
{
"cell_type": "markdown",
"id": "41a22c63",
"metadata": {},
"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."
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "c8ad03e9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"book uptaded now there's {2} left in inventory\n",
"keychain uptaded now there's {4} left in inventory\n",
"mug uptaded now there's {6} left in inventory\n",
"hat uptaded now there's {7} left in inventory\n",
"updated inventory:\n",
"{'t-shirt': 6, 'mug': 6, 'hat': 7, 'book': 2, 'keychain': 4}\n"
]
}
],
"source": [
"def update_inventory (customer_orders, inventory):\n",
" for item in customer_orders:\n",
" if item in inventory:\n",
" if inventory [item] >0:\n",
" inventory [item] -=1\n",
" print (f'{item}',\"uptaded now there's\",{inventory[item]},\"left in inventory\")\n",
" else:\n",
" print (f'{item}', \"is out of stock in inventory\")\n",
" else:\n",
" print (f'{item}', \"does not exist in inventory\")\n",
" return (inventory) \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",
"inventory = update_inventory (customer_orders, inventory)\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",
"print (\"updated inventory:\")\n",
"print (inventory)\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "788c8fd9",
"metadata": {},
"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."
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "24d8ae4e",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics (customer_orders , products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_products_inventory = len (products)\n",
" percentage_of_products_ordered = (total_products_ordered/total_products_inventory)*100\n",
" return (total_products_ordered , percentage_of_products_ordered)\n",
" "
]
},
{
"cell_type": "markdown",
"id": "e00e8c9d",
"metadata": {},
"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."
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "3b5646a1",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total = order_statistics[0]\n",
" percentage = order_statistics[1]\n",
"\n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", total)\n",
" print(\"Percentage of Products Ordered:\", percentage, \"%\")\n"
]
},
{
"cell_type": "markdown",
"id": "08e6cdc7",
"metadata": {},
"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."
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "215f42a2",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for item, quantity in inventory.items():\n",
" print(item, \":\", quantity)\n"
]
},
{
"cell_type": "markdown",
"id": "62793a38",
"metadata": {},
"source": [
"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",
"- Test your functions individually to ensure they work correctly."
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "f554cfe1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initial inventory\n",
"{'t-shirt': 6, 'mug': 7, 'hat': 8, 'book': 9, 'keychain': 4}\n",
"mug added to your order\n",
"iguana is not a product available\n",
"book added to your order\n",
"hat added to your order\n",
"book uptaded now there's {8} left in inventory\n",
"mug uptaded now there's {6} left in inventory\n",
"hat uptaded now there's {7} left in inventory\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0 %\n",
"Updated Inventory:\n",
"t-shirt : 6\n",
"mug : 6\n",
"hat : 7\n",
"book : 8\n",
"keychain : 4\n"
]
}
],
"source": [
"# 1. List of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n"
"# 2. Initialize the inventory (the user enters the quantities)\n",
"inventory = initialize_inventory(products)\n",
"print (\"initial inventory\")\n",
"print (inventory)\n",
"\n",
"# 3. Get the customer's order\n",
"customer_orders = get_customer_orders()\n",
"\n",
"# 4. Update the inventory based on the customer's order\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"# 5. Calculate order statistics\n",
"order_stats = calculate_order_statistics(customer_orders, products)\n",
"\n",
"# 6. Print order statistics and updated inventory\n",
"print_order_statistics(order_stats)\n",
"print_updated_inventory(inventory)\n"
]
}
],
Expand All @@ -61,7 +310,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down