From e8cbcb3ec36902a8cc7a0640d818a510754ab203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tebas=20Mart=C3=ADnez?= <203688711+TebasMartinez@users.noreply.github.com> Date: Wed, 9 Jul 2025 14:21:36 +0200 Subject: [PATCH 1/2] solve lab --- .../lab-python-flow-control-checkpoint.ipynb | 269 ++++++++++++++++++ lab-python-flow-control.ipynb | 212 +++++++++++++- 2 files changed, 478 insertions(+), 3 deletions(-) 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..11c8b94 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -0,0 +1,269 @@ +{ + "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": 1, + "id": "278ac3c2", + "metadata": {}, + "outputs": [], + "source": [ + "# 1\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e60fd14c", + "metadata": {}, + "outputs": [], + "source": [ + "# 2\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "872f0dba", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: 8\n", + "Please provide number of mug available: 14\n", + "Please provide number of hat available: 7\n", + "Please provide number of book available: 9\n", + "Please provide number of keychain available: 32\n" + ] + } + ], + "source": [ + "# 3\n", + "for product in products:\n", + " inventory[product] = input(f\"Please provide number of {product} available: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d84f8926", + "metadata": {}, + "outputs": [], + "source": [ + "# 4\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9a80f6b8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please add product to the order: mug\n", + "Would you like to add another product? (yes/no) yes\n", + "Please add product to the order: hat\n", + "Would you like to add another product? (yes/no) yes\n", + "Please add product to the order: book\n", + "Would you like to add another product? (yes/no) yes\n", + "Please add product to the order: keychain\n", + "Would you like to add another product? (yes/no) no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you for your order!\n" + ] + } + ], + "source": [ + "# 5\n", + "answer = \"yes\"\n", + "while answer != \"no\":\n", + " customer_orders.add(input(f\"Please add product to the order: \"))\n", + " answer = input(\"Would you like to add another product? (yes/no) \")\n", + "if answer == \"no\":\n", + " print(\"Thank you for your order!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "9e644b30", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The products in the order are: {'mug', 'keychain', 'hat', 'book'}\n" + ] + } + ], + "source": [ + "# 6\n", + "print(f\"The products in the order are: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "524ff43e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of products in the order is 4.\n" + ] + } + ], + "source": [ + "# 7\n", + "order_length = len(customer_orders)\n", + "print(f\"The number of products in the order is {order_length}.\")\n", + "\n", + "products_length = len(products)\n", + "\n", + "total_products = 0\n", + "for product in products:\n", + " total_products += int(inventory[product])\n", + "\n", + "perc_ordered = (order_length / total_products) * 100\n", + "\n", + "stats_tuple = (products_length, perc_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "3bf729f7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 4\n", + "Percentage of Products Ordered: 5.71%\n" + ] + } + ], + "source": [ + "# 8\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_length}\")\n", + "print(f\"Percentage of Products Ordered: {round(perc_ordered, 2)}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "ab59108e", + "metadata": {}, + "outputs": [], + "source": [ + "# 9\n", + "for product in customer_orders:\n", + " if product in inventory.keys():\n", + " prod = int(inventory[product])\n", + " prod -= 1\n", + " inventory[product] = str(prod)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "2776901b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 8\n", + "mug: 13\n", + "hat: 6\n", + "book: 8\n", + "keychain: 31\n" + ] + } + ], + "source": [ + "# 10\n", + "for product in products:\n", + " print(f\"{product}: {inventory[product]}\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..11c8b94 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,219 @@ "\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": "278ac3c2", + "metadata": {}, + "outputs": [], + "source": [ + "# 1\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e60fd14c", + "metadata": {}, + "outputs": [], + "source": [ + "# 2\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "872f0dba", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please provide number of t-shirt available: 8\n", + "Please provide number of mug available: 14\n", + "Please provide number of hat available: 7\n", + "Please provide number of book available: 9\n", + "Please provide number of keychain available: 32\n" + ] + } + ], + "source": [ + "# 3\n", + "for product in products:\n", + " inventory[product] = input(f\"Please provide number of {product} available: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d84f8926", + "metadata": {}, + "outputs": [], + "source": [ + "# 4\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9a80f6b8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please add product to the order: mug\n", + "Would you like to add another product? (yes/no) yes\n", + "Please add product to the order: hat\n", + "Would you like to add another product? (yes/no) yes\n", + "Please add product to the order: book\n", + "Would you like to add another product? (yes/no) yes\n", + "Please add product to the order: keychain\n", + "Would you like to add another product? (yes/no) no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you for your order!\n" + ] + } + ], + "source": [ + "# 5\n", + "answer = \"yes\"\n", + "while answer != \"no\":\n", + " customer_orders.add(input(f\"Please add product to the order: \"))\n", + " answer = input(\"Would you like to add another product? (yes/no) \")\n", + "if answer == \"no\":\n", + " print(\"Thank you for your order!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "9e644b30", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The products in the order are: {'mug', 'keychain', 'hat', 'book'}\n" + ] + } + ], + "source": [ + "# 6\n", + "print(f\"The products in the order are: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "524ff43e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of products in the order is 4.\n" + ] + } + ], + "source": [ + "# 7\n", + "order_length = len(customer_orders)\n", + "print(f\"The number of products in the order is {order_length}.\")\n", + "\n", + "products_length = len(products)\n", + "\n", + "total_products = 0\n", + "for product in products:\n", + " total_products += int(inventory[product])\n", + "\n", + "perc_ordered = (order_length / total_products) * 100\n", + "\n", + "stats_tuple = (products_length, perc_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "3bf729f7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 4\n", + "Percentage of Products Ordered: 5.71%\n" + ] + } + ], + "source": [ + "# 8\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_length}\")\n", + "print(f\"Percentage of Products Ordered: {round(perc_ordered, 2)}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "ab59108e", + "metadata": {}, + "outputs": [], + "source": [ + "# 9\n", + "for product in customer_orders:\n", + " if product in inventory.keys():\n", + " prod = int(inventory[product])\n", + " prod -= 1\n", + " inventory[product] = str(prod)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "2776901b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 8\n", + "mug: 13\n", + "hat: 6\n", + "book: 8\n", + "keychain: 31\n" + ] + } + ], + "source": [ + "# 10\n", + "for product in products:\n", + " print(f\"{product}: {inventory[product]}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -55,7 +261,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, From c77f84934e9fddbb4469fd3e696da33db48a29ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tebas=20Mart=C3=ADnez?= <203688711+TebasMartinez@users.noreply.github.com> Date: Thu, 10 Jul 2025 17:44:02 +0200 Subject: [PATCH 2/2] add comments from lab review --- .../lab-python-flow-control-checkpoint.ipynb | 10 +++++----- lab-python-flow-control.ipynb | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb index 11c8b94..212bda8 100644 --- a/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb +++ b/.ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb @@ -129,7 +129,7 @@ "while answer != \"no\":\n", " customer_orders.add(input(f\"Please add product to the order: \"))\n", " answer = input(\"Would you like to add another product? (yes/no) \")\n", - "if answer == \"no\":\n", + "if answer == \"no\": # \"else\" is also possible after the while loop\n", " print(\"Thank you for your order!\")" ] }, @@ -173,7 +173,7 @@ "\n", "products_length = len(products)\n", "\n", - "total_products = 0\n", + "total_products = 0 # more efficient: sum(inventory.values())\n", "for product in products:\n", " total_products += int(inventory[product])\n", "\n", @@ -215,9 +215,9 @@ "# 9\n", "for product in customer_orders:\n", " if product in inventory.keys():\n", - " prod = int(inventory[product])\n", - " prod -= 1\n", - " inventory[product] = str(prod)" + " qty = int(inventory[product])\n", + " qty -= 1\n", + " inventory[product] = str(qty)" ] }, { diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 11c8b94..212bda8 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -129,7 +129,7 @@ "while answer != \"no\":\n", " customer_orders.add(input(f\"Please add product to the order: \"))\n", " answer = input(\"Would you like to add another product? (yes/no) \")\n", - "if answer == \"no\":\n", + "if answer == \"no\": # \"else\" is also possible after the while loop\n", " print(\"Thank you for your order!\")" ] }, @@ -173,7 +173,7 @@ "\n", "products_length = len(products)\n", "\n", - "total_products = 0\n", + "total_products = 0 # more efficient: sum(inventory.values())\n", "for product in products:\n", " total_products += int(inventory[product])\n", "\n", @@ -215,9 +215,9 @@ "# 9\n", "for product in customer_orders:\n", " if product in inventory.keys():\n", - " prod = int(inventory[product])\n", - " prod -= 1\n", - " inventory[product] = str(prod)" + " qty = int(inventory[product])\n", + " qty -= 1\n", + " inventory[product] = str(qty)" ] }, {