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
101 changes: 75 additions & 26 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
@@ -1,55 +1,104 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"cell_type": "code",
"execution_count": 1,
"id": "f8a58a14-62b1-469a-90c3-daf1e6384233",
"metadata": {},
"outputs": [],
"source": [
"# Lab | Functions"
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"cell_type": "code",
"execution_count": null,
"id": "077c47cf-d0ee-440d-a66f-5f689fec8b8d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity of each product:\n"
]
}
],
"source": [
"## Exercise: Managing Customer Orders with Functions\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" print(\"Enter the quantity of each product:\")\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\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",
" return inventory\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" item = input(f\"Enter product you want to order ['t-shirt', 'mug', 'hat', 'book', 'keychain']: \")\n",
" while item not in products:\n",
" item = input(\"❌Invalid product.Please choose from this list ['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",
" customer_orders.add(item)\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",
" another = input(\"Do you want to order another product? (yes/no): \")\n",
" if another != \"yes\":\n",
" break\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",
" return 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",
"def update_inventory(inventory, customer_orders):\n",
" for item in customer_orders:\n",
" if inventory[item] > 0:\n",
" inventory[item] -= 1\n",
" else:\n",
" print(f\"The product {item} is not available in the 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",
" return 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",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = total_products_ordered / len(products) * 100\n",
" order_status = (total_products_ordered, percentage_ordered)\n",
" # return total_products_ordered, percentage_ordered\n",
" return order_status\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", order_statistics[0])\n",
" print(\"Percentage of Products Ordered:\", order_statistics[1], \"%\")\n",
"\n",
"Hints for functions:\n",
"def print_updated_inventory(inventory):\n",
" print(\"Inventory after order:\")\n",
" for item in inventory:\n",
" print(item, inventory[item])\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"
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"inventory = update_inventory(inventory, customer_orders)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f4c1505-d580-4846-a1cf-45e9e10b013b",
"metadata": {},
"outputs": [],
"source": []
}
],
"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 +110,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down