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
141 changes: 126 additions & 15 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,148 @@
"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",
"In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's go one 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",
"1. Define a function called `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code to initialize 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",
"2. Define a function called `get_customer_orders` that does not accept parameters. Inside the function, implement the code to prompt the user to enter product names using a loop. The function should return the `customer_orders` array.\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",
"3. Define a function called `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code to update the inventory dictionary based on 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",
"4. Define a function called `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement code to calculate `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",
"5. Define a function called `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code to print 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",
"6. Define a function called `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code to print the updated inventory.\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"7. Call functions in the proper sequence to run the program and handle customer orders.\n",
"\n",
"Hints for functions:\n",
"Tips 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",
"- Consider the input parameters needed for each function and their return values.\n",
"- Use function parameters and return values ​​to transfer data between functions.\n",
"- Test your features individually to make sure they work correctly."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a8e484e7",
"metadata": {},
"outputs": [],
"source": [
"products = []\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" \n",
" for p in products:\n",
" inventory[p] = int(input(\"que cantidad de producto hay?\"))\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "874377b3",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" añadir_producto = \"yes\"\n",
" customer_orders = set()\n",
"\n",
" while añadir_producto == \"yes\":\n",
" producto = input(\"ingrese una opcion de entre : t-shirt, mug, hat, book, or keychain\").lower()\n",
" customer_orders.add(producto)\n",
" añadir_producto = input(\"quiere añadir otro producto? (yes/no):\").lower()\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "0b2f9f38",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" \n",
" for x in customer_orders:\n",
" inventory[x] = inventory[x] -1\n",
"\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a293ef6d",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders,products):\n",
" order_statistics = []\n",
"\n",
"\n"
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (len(customer_orders) / len(products)) * 100\n",
" order_statistics = total_products_ordered,f\"{percentage_ordered} %\"\n",
" return order_statistics "
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "5edb6c21",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "58c24ee1",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "b9bee4a4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, '60.0 %')\n",
"{'t-shirt': 1, 'mug': 1, 'hat': 2, 'book': 3, 'keychain': 5}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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_order_statistics(inventory) "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +172,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down