From f45b76aab0731734e7f6aa8fe5d1f1232f7ef43e Mon Sep 17 00:00:00 2001 From: claudiasanchez07 Date: Fri, 10 Oct 2025 16:50:45 +0200 Subject: [PATCH] flow control solution --- lab-python-flow-control-solutions.ipynb | 164 ++++++++++++++++++++++++ lab-python-flow-control.ipynb | 63 --------- 2 files changed, 164 insertions(+), 63 deletions(-) create mode 100644 lab-python-flow-control-solutions.ipynb delete mode 100644 lab-python-flow-control.ipynb diff --git a/lab-python-flow-control-solutions.ipynb b/lab-python-flow-control-solutions.ipynb new file mode 100644 index 0000000..a78a99f --- /dev/null +++ b/lab-python-flow-control-solutions.ipynb @@ -0,0 +1,164 @@ +{ + "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\")." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "3d17cd44", + "metadata": {}, + "outputs": [], + "source": [ + "products= [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory= {}\n", + "t_shirt=int(input(\"Enter the quantity of t-shirt:\"))\n", + "mug=int(input(\"Enter the quantity of mug:\"))\n", + "hat= int(input(\"Number of hats?\"))\n", + "book= int(input(\"Number of books?\"))\n", + "keychain= int(input(\"Number of keychains?\"))\n", + "inventory['t-shirt']=t_shirt\n", + "inventory['mug']=mug\n", + "inventory['hat']=hat\n", + "inventory['book']=book\n", + "inventory['keychain']=keychain\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d24db976", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d524a6bf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n", + "{'mug'}\n", + "You just added a mug\n", + "You just added a hat\n", + "You just added a book\n", + "No more items added\n", + "{'mug', 'book', 'hat'}\n", + "3\n", + "The percentage is 60.0%\n", + "(60.0,)\n", + "Order Statistics:\n", + "Total Products Ordered:3\n", + "Percentage of Products Ordered:60.0%\n", + "t-shirt 5\n", + "mug 4\n", + "hat 4\n", + "book 4\n", + "keychain 5\n" + ] + } + ], + "source": [ + "print(inventory)\n", + "customer_orders= set()\n", + "my_list=input(\"Which item do you want? t-shirt, mug, hat, book or keychain?\").lower()\n", + "customer_orders.add(my_list)\n", + "print(customer_orders)\n", + "add_order=input(\"Do you want to add another product? (yes/no)\").strip().lower()\n", + "add_order=(add_order==\"yes\")\n", + "while True:\n", + " add_item=input(\"Which item do you want to add?\").lower()\n", + " customer_orders.add(add_item)\n", + " print(f\"You just added a {add_item}\")\n", + " last_add=input (\"Do you want to add something more?(yes/no)\").strip().lower()\n", + " is_true=(last_add==\"yes\")\n", + " #last_add se convierte en bool al hacerlo ==\"yes\"\n", + " if not is_true:\n", + " print(\"No more items added\")\n", + " break\n", + " \n", + "print(customer_orders)\n", + "total_products_ordered=len(customer_orders)\n", + "print(total_products_ordered)\n", + "percentage= len(customer_orders)/len(products)*100\n", + "print( f\"The percentage is {percentage}%\")\n", + "order_status=(percentage,)\n", + "print(order_status)\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered:{total_products_ordered}\")\n", + "print(f\"Percentage of Products Ordered:{percentage}%\")\n", + "\n", + "for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item]-=1\n", + "\n", + "for key, value in inventory.items():\n", + " print(key,value)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb deleted file mode 100644 index f4c7391..0000000 --- a/lab-python-flow-control.ipynb +++ /dev/null @@ -1,63 +0,0 @@ -{ - "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 -}