From 267cbf7faf7d4384fc652884b4483255629add07 Mon Sep 17 00:00:00 2001 From: slmj1990-ai Date: Sat, 17 Jan 2026 21:34:40 +0100 Subject: [PATCH] Practica funciones --- .../lab-python-functions-checkpoint.ipynb | 69 ++++++++++ lab-python-functions.ipynb | 119 +++++++++++++++++- 2 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..44d337b --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,69 @@ +{ + "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" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..7530ab3 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,123 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7269d9fb-b54a-4677-8167-9152e45ae450", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Select the quantity of t-shirts to add to inventory: 57\n", + "Select the quantity of mugs to add to inventory: 68\n", + "Select the quantity of hats to add to inventory: 15\n", + "Select the quantity of books to add to inventory: 64\n", + "Select the quantity of keychains to add to inventory: 74\n", + "Enter the name of a product the customer wants to order: book\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product the customer wants to order: hat\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Products in customer orders: {'book', 'hat'}\n", + "\n", + "Order Statistics\n", + "Total products ordered: 2\n", + "Percentage of products ordered: 40.0\n", + "\n", + "Updated inventory:\n", + "T-Shirt: 57\n", + "Mug: 68\n", + "Hat: 14\n", + "Book: 63\n", + "Keychain: 74\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Select the quantity of {product}s to add to inventory: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " product_name = input(\"Enter the name of a product the customer wants to order: \")\n", + " customer_orders.add(product_name)\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if another != \"yes\":\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product_name in customer_orders:\n", + " if product_name in inventory:\n", + " if inventory[product_name] > 0:\n", + " inventory[product_name] -= 1\n", + " else:\n", + " print(f\"{product_name} is out of stock.\")\n", + " else:\n", + " print(f\"Product '{product_name}' not found in inventory.\")\n", + "\n", + "\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", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"\\nOrder Statistics\")\n", + " print(\"Total products ordered:\", total_products_ordered)\n", + " print(\"Percentage of products ordered:\", percentage_ordered)\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product.title()}: {quantity}\")\n", + "\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "\n", + "print(\"\\nProducts in customer orders:\", customer_orders)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "58d68b8a-e310-453c-bda5-faad6fc76f68", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +178,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,