From 3ae6312b34af67b4807092fac2047659cedae9e1 Mon Sep 17 00:00:00 2001 From: Juan Carlos Del Mar Lostanau <147656412+Juancarlosdelmarlostanau@users.noreply.github.com> Date: Wed, 16 Sep 2026 00:35:36 +0200 Subject: [PATCH 1/2] Update lab-python-functions.ipynb --- lab-python-functions.ipynb | 244 ++++++++++++++++++++++++++++++++++--- 1 file changed, 229 insertions(+), 15 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..31a1891 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -17,37 +17,251 @@ "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": 2, + "id": "a8e484e7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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", + "\n", + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "2b0b2cb4", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "874377b3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'hat'}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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\n", + "\n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "139bbfc9", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "0b2f9f38", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 2, 'book': 4, 'keychain': 5}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " \n", + " for x in customer_orders:\n", + " if x in inventory:\n", + " inventory[x] -= 1\n", "\n", - "\n" + " return inventory\n", + "\n", + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "a293ef6d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, '20.0 %')" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_order_statistics(customer_orders,products):\n", + " order_statistics = []\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\n", + "\n", + "calculate_order_statistics(customer_orders,products) " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "98752819", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders,products)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "5edb6c21", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, '20.0 %')\n" + ] + } + ], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(order_statistics)\n", + " \n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "58c24ee1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 2, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(inventory)\n", + " \n", + "print_order_statistics(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "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" }, @@ -61,7 +275,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4, From ce0a094358e09b7be05857e8af3fe156801c01fb Mon Sep 17 00:00:00 2001 From: Juan Carlos Del Mar Lostanau <147656412+Juancarlosdelmarlostanau@users.noreply.github.com> Date: Wed, 16 Sep 2026 10:08:58 +0200 Subject: [PATCH 2/2] Update lab-python-functions.ipynb solucion 2 --- lab-python-functions.ipynb | 145 ++++++------------------------------- 1 file changed, 21 insertions(+), 124 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 31a1891..f2056fa 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -44,61 +44,27 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "id": "a8e484e7", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "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", - "\n", - "initialize_inventory(products)" + " return inventory\n" ] }, { "cell_type": "code", - "execution_count": 8, - "id": "2b0b2cb4", - "metadata": {}, - "outputs": [], - "source": [ - "inventory = initialize_inventory(products)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, + "execution_count": 6, "id": "874377b3", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'hat'}" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def get_customer_orders():\n", " añadir_producto = \"yes\"\n", @@ -108,67 +74,30 @@ " 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\n", - "\n", - "get_customer_orders()" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "139bbfc9", - "metadata": {}, - "outputs": [], - "source": [ - "customer_orders = get_customer_orders()" + " return customer_orders" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 7, "id": "0b2f9f38", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'t-shirt': 1, 'mug': 2, 'hat': 2, 'book': 4, 'keychain': 5}" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def update_inventory(customer_orders, inventory):\n", " \n", " for x in customer_orders:\n", - " if x in inventory:\n", - " inventory[x] -= 1\n", + " inventory[x] = inventory[x] -1\n", "\n", - " return inventory\n", - "\n", - "update_inventory(customer_orders, inventory)" + " return inventory\n" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 9, "id": "a293ef6d", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(1, '20.0 %')" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def calculate_order_statistics(customer_orders,products):\n", " order_statistics = []\n", @@ -176,66 +105,34 @@ " 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\n", - "\n", - "calculate_order_statistics(customer_orders,products) " + " return order_statistics " ] }, { "cell_type": "code", - "execution_count": 13, - "id": "98752819", - "metadata": {}, - "outputs": [], - "source": [ - "order_statistics = calculate_order_statistics(customer_orders,products)" - ] - }, - { - "cell_type": "code", - "execution_count": 14, + "execution_count": 10, "id": "5edb6c21", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1, '20.0 %')\n" - ] - } - ], + "outputs": [], "source": [ "def print_order_statistics(order_statistics):\n", - " print(order_statistics)\n", - " \n", - "print_order_statistics(order_statistics)" + " print(order_statistics)" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 11, "id": "58c24ee1", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'t-shirt': 1, 'mug': 2, 'hat': 2, 'book': 4, 'keychain': 5}\n" - ] - } - ], + "outputs": [], "source": [ "def print_updated_inventory(inventory):\n", - " print(inventory)\n", - " \n", - "print_order_statistics(inventory)" + " print(inventory)" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 13, "id": "b9bee4a4", "metadata": {}, "outputs": [