From 9b7df4c4984627685f08cccb99104c4965a5cd84 Mon Sep 17 00:00:00 2001 From: slmj1990-ai Date: Sat, 17 Jan 2026 21:21:40 +0100 Subject: [PATCH] practica Flow_control --- .../lab-python-flow-control-checkpoint.ipynb | 63 +++++++++++ lab-python-flow-control.ipynb | 101 +++++++++++++++++- 2 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb new file mode 100644 index 0000000..f4c7391 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,63 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "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", + "\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\")." + ] + } + ], + "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-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..d1a1000 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,105 @@ "\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": "code", + "execution_count": 1, + "id": "6b19706c", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Select the quantity of t-shirts to add to inventory: 75\n", + "Select the quantity of mugs to add to inventory: 108\n", + "Select the quantity of hats to add to inventory: 99\n", + "Select the quantity of books to add to inventory: 82\n", + "Select the quantity of keychains to add to inventory: 114\n", + "Enter the name of a product the customer wants to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product the customer wants to order: book\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Products in customer orders: {'mug', 'book'}\n", + "\n", + "Order Statistics\n", + "Total products ordered: 2\n", + "Percentage of products ordered: 40.0\n", + "\n", + "Updated inventory:\n", + "T-Shirt: 75\n", + "Mug: 107\n", + "Hat: 99\n", + "Book: 81\n", + "Keychain: 114\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Creacion de inventario \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", + "\n", + "# Ordenes de clientes con loops\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", + "print(\"\\nProducts in customer orders:\", customer_orders)\n", + "\n", + "#totales\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "print(\"\\nOrder Statistics\")\n", + "print(\"Total products ordered:\", total_products_ordered)\n", + "print(\"Percentage of products ordered:\", percentage_ordered)\n", + "\n", + "\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", + "print(\"\\nUpdated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product.title()}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "faba5a4e-dbd6-4a64-a8a0-ac5a0d52d276", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +154,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,