Skip to content
Open
Show file tree
Hide file tree
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
205 changes: 205 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"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": "code",
"execution_count": null,
"id": "df38dc20-20f2-4fa5-a38a-836e1e9e9176",
"metadata": {},
"outputs": [],
"source": [
"# Lista de todos los productos que vende la tienda\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# El inventario y los pedidos se crearán y gestionarán dentro de las funciones.\n",
"print(\"Productos disponibles:\", products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b46ee707-e231-47fd-8a0c-de34976197f8",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory (products):\n",
" inventory = {}\n",
" for product in products:\n",
" user_input = input('Please enter a quantity of a product:')\n",
" quantity = int(user_input)\n",
" inventory[product] = quantity\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2bd73f0-920a-41d5-ad20-07a8f854a1a7",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders ():\n",
" customer_orders = set()\n",
" adding_products = 'yes'\n",
" while adding_products == 'yes':\n",
" user_input = input('Please enter a product:')\n",
" customer_orders.add(user_input)\n",
" user_input_2 = input('Would you like to add another product?:').lower()\n",
" adding_products = user_input_2\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6420f9db-6e43-42b1-b6dd-9dbcaf7cc262",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory (customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "952396a5-461c-4aa5-9b09-4894a4d192ff",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" \n",
" total_products_ordered = len(customer_orders)\n",
" \n",
" total_available_products = len(products)\n",
" \n",
" percentage_unique_ordered = (total_products_ordered / total_available_products) * 100\n",
" # Retorna la tupla con las dos métricas\n",
" return total_products_ordered, percentage_unique_ordered\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e861333b-09ab-402e-a70a-2168dbec29bc",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics (order_statistics):\n",
" total_products_ordered, percentage_unique_ordered = order_statistics\n",
" print(f'These are the total products ordered: {total_products_ordered}')\n",
" print(f'This is the percentage of unique products: {percentage_unique_ordered}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b91b530b-716a-43be-bb37-e882ca8545c6",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory (inventory):\n",
" print(f'This is the inventory updated:{inventory}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42fb10af-7aff-45a0-8496-c2dd0472fada",
"metadata": {},
"outputs": [],
"source": [
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] \n",
"\n",
"current_inventory = initialize_inventory(products)\n",
"\n",
"customer_orders_set = get_customer_orders()\n",
"\n",
"updated_inventory = update_inventory(customer_orders_set, current_inventory)\n",
"\n",
"order_stats = calculate_order_statistics(customer_orders_set, products)\n",
"\n",
"print_order_statistics(order_stats)\n",
"\n",
"print_updated_inventory(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "815882ca-2eb1-43c2-be2a-dbe9ec7f90e5",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
138 changes: 137 additions & 1 deletion lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,142 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df38dc20-20f2-4fa5-a38a-836e1e9e9176",
"metadata": {},
"outputs": [],
"source": [
"# Lista de todos los productos que vende la tienda\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# El inventario y los pedidos se crearán y gestionarán dentro de las funciones.\n",
"print(\"Productos disponibles:\", products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b46ee707-e231-47fd-8a0c-de34976197f8",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory (products):\n",
" inventory = {}\n",
" for product in products:\n",
" user_input = input('Please enter a quantity of a product:')\n",
" quantity = int(user_input)\n",
" inventory[product] = quantity\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2bd73f0-920a-41d5-ad20-07a8f854a1a7",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders ():\n",
" customer_orders = set()\n",
" adding_products = 'yes'\n",
" while adding_products == 'yes':\n",
" user_input = input('Please enter a product:')\n",
" customer_orders.add(user_input)\n",
" user_input_2 = input('Would you like to add another product?:').lower()\n",
" adding_products = user_input_2\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6420f9db-6e43-42b1-b6dd-9dbcaf7cc262",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory (customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "952396a5-461c-4aa5-9b09-4894a4d192ff",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" \n",
" total_products_ordered = len(customer_orders)\n",
" \n",
" total_available_products = len(products)\n",
" \n",
" percentage_unique_ordered = (total_products_ordered / total_available_products) * 100\n",
" # Retorna la tupla con las dos métricas\n",
" return total_products_ordered, percentage_unique_ordered\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e861333b-09ab-402e-a70a-2168dbec29bc",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics (order_statistics):\n",
" total_products_ordered, percentage_unique_ordered = order_statistics\n",
" print(f'These are the total products ordered: {total_products_ordered}')\n",
" print(f'This is the percentage of unique products: {percentage_unique_ordered}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b91b530b-716a-43be-bb37-e882ca8545c6",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory (inventory):\n",
" print(f'This is the inventory updated:{inventory}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42fb10af-7aff-45a0-8496-c2dd0472fada",
"metadata": {},
"outputs": [],
"source": [
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] \n",
"\n",
"current_inventory = initialize_inventory(products)\n",
"\n",
"customer_orders_set = get_customer_orders()\n",
"\n",
"updated_inventory = update_inventory(customer_orders_set, current_inventory)\n",
"\n",
"order_stats = calculate_order_statistics(customer_orders_set, products)\n",
"\n",
"print_order_statistics(order_stats)\n",
"\n",
"print_updated_inventory(updated_inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "815882ca-2eb1-43c2-be2a-dbe9ec7f90e5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -61,7 +197,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down