diff --git a/segundo intento/lab-python-functions.ipynb b/segundo intento/lab-python-functions.ipynb new file mode 100644 index 0000000..24d9a20 --- /dev/null +++ b/segundo intento/lab-python-functions.ipynb @@ -0,0 +1,697 @@ +{ + "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": "markdown", + "id": "35556a96-5068-4440-9544-7ea7d7a69755", + "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": 29, + "id": "9d202361-2b75-4bf1-a944-8319d5acaec8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter the quantity of t-shirt: 6\n", + "enter the quantity of mug: 8\n", + "enter the quantity of hat: 9\n", + "enter the quantity of book: 19\n", + "enter the quantity of keychain: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " the inventory is: {'t-shirt': 6, 'mug': 8, 'hat': 9, 'book': 19, 'keychain': 1}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "def initialize_inventory(products):\n", + " \n", + " \n", + " for product in products:\n", + " quantity = int(input(f\"enter the quantity of {product}:\"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "my_inventory = initialize_inventory(products)\n", + "print(f\" the inventory is: {my_inventory}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "7b08eb05-ff2d-4f76-9c3a-6fbe4cbfbc21", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 8, 'hat': 9, 'book': 19, 'keychain': 1}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "3a997e10-c827-4a59-a4eb-c582f62006ef", + "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": 31, + "id": "73184c42-c0a5-45d0-a260-68b3614e0a84", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product for the order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'hat' to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try adding another item? (yes/no): yes\n", + "Enter product for the order: dog\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, 'dog' is not in our product list.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try adding another item? (yes/no): yes\n", + "Enter product for the order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'book' to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try adding another item? (yes/no): yes\n", + "Enter product for the order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'mug' to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try adding another item? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your final order set: {'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = set()\n", + "def get_customer_orders():\n", + "\n", + " ordering = True\n", + " \n", + " while ordering:\n", + " order = input(\"Enter product for the order: \")\n", + "\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " print(f\"Added '{order}' to your order.\")\n", + " else:\n", + " print(f\"Sorry, '{order}' is not in our product list.\")\n", + "\n", + " while True:\n", + " add_order = input(\"Do you want to try adding another item? (yes/no): \").lower()\n", + " \n", + " if add_order == \"yes\":\n", + " break \n", + " elif add_order == \"no\":\n", + " ordering = False \n", + " break \n", + " else:\n", + " print(\"Please enter 'yes' or 'no'.\")\n", + "\n", + "\n", + " return customer_orders\n", + "\n", + "print(f\"Your final order set: {get_customer_orders()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "33465989-f30a-477a-a020-584723870d25", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "d30585c7-1d1f-4044-8c5f-3a62ffd01cd2", + "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": 45, + "id": "1a37124f-ce57-4719-a014-7f5eef1a8334", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory): \n", + " for products in customer_orders:\n", + " inventory[products] -=1\n", + " print(f\"inventory updated: {products} now has {inventory[products]} items\")\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "101d7e8a-c5fc-4163-aa11-8f3afd3f5858", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "inventory updated: hat now has 6 items\n", + "inventory updated: mug now has 7 items\n", + "inventory updated: book now has 18 items\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 7, 'hat': 6, 'book': 18, 'keychain': 1}" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "1dffa5d0-a5fc-4b43-a988-7854501d6c36", + "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": 72, + "id": "f522bfef-862d-4c83-b568-608d630581d0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Unique Products Ordered: 3\n", + "Percentage of Catalog Ordered: 60.0%\n" + ] + } + ], + "source": [ + "\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", + " return total_products_ordered, percentage_ordered\n", + "total, percentage = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print(f\"Total Unique Products Ordered: {total}\")\n", + "print(f\"Percentage of Catalog Ordered: {percentage}%\")" + ] + }, + { + "cell_type": "markdown", + "id": "9df09950-fc0c-45a7-ba3d-e2906f716a65", + "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.\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "4b9de0ae-0d5e-4575-9403-d169fc0e8356", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(f\"Total Unique Products Ordered: {total}\")\n", + " print(f\"Percentage of Catalog Ordered: {percentage}%\")\n", + " return order_statistics\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "3734d3d4-6b25-4fa7-831e-4ac0aa1a71fd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Unique Products Ordered: 3\n", + "Percentage of Catalog Ordered: 60.0%\n" + ] + }, + { + "data": { + "text/plain": [ + "()" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "markdown", + "id": "cdc476ec-bcd4-4495-8ed5-acb9c7c8eef2", + "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": 91, + "id": "e2516f89-7f42-45bc-8c72-561e37cb32aa", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory Status:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"Product: {product:12} Remaining Stock: {quantity}\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "502ce078-5212-4551-aafc-e2d232a7de9e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory Status:\n", + "Product: t-shirt Remaining Stock: 7\n", + "Product: mug Remaining Stock: 8\n", + "Product: hat Remaining Stock: 4\n", + "Product: book Remaining Stock: 2\n", + "Product: keychain Remaining Stock: 1\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "808c9ae8-e120-4a37-b726-a42c6aac99cd", + "metadata": {}, + "source": [ + "7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "1916ac26-e219-4722-aa11-99a13d534250", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter the quantity of t-shirt: 7\n", + "enter the quantity of mug: 9\n", + "enter the quantity of hat: 5\n", + "enter the quantity of book: 3\n", + "enter the quantity of keychain: 1\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 7, 'mug': 9, 'hat': 5, 'book': 3, 'keychain': 1}" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "7915f6ba-a4bb-4b6e-b568-ea94a5df09cc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product for the order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'hat' to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try adding another item? (yes/no): yes\n", + "Enter product for the order: dog\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, 'dog' is not in our product list.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try adding another item? (yes/no): yes\n", + "Enter product for the order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'mug' to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try adding another item? (yes/no): yes\n", + "Enter product for the order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 'book' to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to try adding another item? (yes/no): no\n" + ] + }, + { + "data": { + "text/plain": [ + "{'book', 'hat', 'mug'}" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "b0ce7fcd-7f2f-4312-a265-5892e11e2710", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "inventory updated: hat now has 4 items\n", + "inventory updated: mug now has 8 items\n", + "inventory updated: book now has 2 items\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 7, 'mug': 8, 'hat': 4, 'book': 2, 'keychain': 1}" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "251070e8-1e22-4a86-84ed-3d38ead1f783", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(3, 60.0)" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "7a6e852a-87bf-4bf1-ae08-60096956256b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Unique Products Ordered: 3\n", + "Percentage of Catalog Ordered: 60.0%\n" + ] + }, + { + "data": { + "text/plain": [ + "()" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "cb191fc6-8c92-410f-b8e6-f5c021aa119d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory Status:\n", + "Product: t-shirt Remaining Stock: 7\n", + "Product: mug Remaining Stock: 8\n", + "Product: hat Remaining Stock: 4\n", + "Product: book Remaining Stock: 2\n", + "Product: keychain Remaining Stock: 1\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "150c19fc-52c5-466a-955e-4f1a08b9f02a", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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 +}