From 800bf78fcdf407b82137508a1bb8e10509e34974 Mon Sep 17 00:00:00 2001 From: aroa Date: Wed, 22 Jul 2026 10:45:41 +0200 Subject: [PATCH 1/2] Solved lab --- lab-python-flow-control.ipynb | 215 ++++++++++++++++++++++++++++------ 1 file changed, 180 insertions(+), 35 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..720b9f4 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -2,60 +2,205 @@ "cells": [ { "cell_type": "markdown", - "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", - "metadata": { - "tags": [] - }, + "metadata": {}, "source": [ - "# Lab | Flow Control" + "# Lab | Flow Control\n", + "\n", + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "Retomamos el lab de Data Structures y lo mejoramos con control de flujo: bucles para evitar repetición y un `while` para pedir productos sin límite fijo." ] }, { "cell_type": "markdown", - "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", "metadata": {}, "source": [ - "## Exercise: Managing Customer Orders Optimized\n", - "\n", - "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", - "\n", - "You did so without using flow control. Let's go a step further and improve this code.\n", - "\n", - "Follow the steps below to complete the exercise:\n", - "\n", - "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "**1.** Lista de productos disponibles (igual que antes)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.** Diccionario vacío para el inventario." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**3.** Pedimos la cantidad disponible de cada producto con un bucle `for` (esto ya usaba flow control en el lab anterior)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**4.** Set vacío para los pedidos del cliente." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**5. (CAMBIO PRINCIPAL)** Antes pedíamos siempre 3 productos con `for _ in range(3)`. Ahora usamos un `while True` que:\n", + "- pregunta el nombre de un producto y lo añade al set,\n", + "- pregunta si el cliente quiere añadir otro (yes/no),\n", + "- repite hasta que la respuesta no sea \"yes\".\n", "\n", - "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", - " \n", - " a. Prompt the user to enter the name of a product that a customer wants to order.\n", - " \n", - " b. Add the product name to the \"customer_orders\" set.\n", - " \n", - " c. Ask the user if they want to add another product (yes/no).\n", - " \n", - " d. Continue the loop until the user does not want to add another product.\n", + "`.lower().strip()` evita errores si el usuario escribe \"Yes\", \" yes \", etc." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "while True:\n", + " order = input(\"Enter the name of a product the customer wants to order: \")\n", + " customer_orders.add(order)\n", "\n", - "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." + " another = input(\"Do you want to add another product? (yes/no): \")\n", + " if another.lower().strip() != \"yes\":\n", + " break" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**6.** Imprimimos los productos pedidos." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Products in customer_orders:\", customer_orders)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**7.** Estadísticas del pedido en una tupla `order_status`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**8.** Imprimimos las estadísticas." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]}%\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**9.** Actualizamos el inventario restando 1 **solo** a los productos pedidos (esto ya lo hacía bien el lab anterior, porque el `for` recorre `customer_orders`, no `products`)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**10.** Imprimimos el inventario actualizado." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "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" + "version": "3.x" } }, "nbformat": 4, From fefb28fba5927ca96e031aed5dbfce43abf92ec0 Mon Sep 17 00:00:00 2001 From: aroa Date: Wed, 22 Jul 2026 10:49:28 +0200 Subject: [PATCH 2/2] Standardize lab instructions to official English text --- lab-python-flow-control.ipynb | 54 +++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 720b9f4..db0e9a0 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -4,18 +4,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Lab | Flow Control\n", - "\n", + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "## Exercise: Managing Customer Orders Optimized\n", "\n", - "Retomamos el lab de Data Structures y lo mejoramos con control de flujo: bucles para evitar repetición y un `while` para pedir productos sin límite fijo." + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**1.** Lista de productos disponibles (igual que antes)." + "**1.** `products` list." ] }, { @@ -31,7 +54,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**2.** Diccionario vacío para el inventario." + "**2.** Empty `inventory` dictionary." ] }, { @@ -47,7 +70,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**3.** Pedimos la cantidad disponible de cada producto con un bucle `for` (esto ya usaba flow control en el lab anterior)." + "**3.** Ask for the quantity of each product with a `for` loop." ] }, { @@ -65,7 +88,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**4.** Set vacío para los pedidos del cliente." + "**4.** Empty `customer_orders` set." ] }, { @@ -81,12 +104,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**5. (CAMBIO PRINCIPAL)** Antes pedíamos siempre 3 productos con `for _ in range(3)`. Ahora usamos un `while True` que:\n", - "- pregunta el nombre de un producto y lo añade al set,\n", - "- pregunta si el cliente quiere añadir otro (yes/no),\n", - "- repite hasta que la respuesta no sea \"yes\".\n", - "\n", - "`.lower().strip()` evita errores si el usuario escribe \"Yes\", \" yes \", etc." + "**5.** Instead of a fixed `for _ in range(3)`, use a `while True` loop: ask for a product, add it, then ask \"add another? (yes/no)\" and stop when the answer isn't yes." ] }, { @@ -108,7 +126,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**6.** Imprimimos los productos pedidos." + "**6.** Print the products in `customer_orders`." ] }, { @@ -124,7 +142,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**7.** Estadísticas del pedido en una tupla `order_status`." + "**7.** Order statistics stored in the `order_status` tuple." ] }, { @@ -142,7 +160,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**8.** Imprimimos las estadísticas." + "**8.** Print the order statistics." ] }, { @@ -160,7 +178,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**9.** Actualizamos el inventario restando 1 **solo** a los productos pedidos (esto ya lo hacía bien el lab anterior, porque el `for` recorre `customer_orders`, no `products`)." + "**9.** Update the inventory, subtracting 1 only for the products that were actually ordered." ] }, { @@ -178,7 +196,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**10.** Imprimimos el inventario actualizado." + "**10.** Print the updated inventory." ] }, {