From 96a35e7335659cd3b2d8b0d6bd345d2a7d92ac7e Mon Sep 17 00:00:00 2001 From: Antonio Gouveia Date: Mon, 1 Sep 2025 10:09:08 +0100 Subject: [PATCH] Solved lab-python-flow-control.ipynb --- lab-python-flow-control.ipynb | 332 +++++++++++++++++++++++++++++++++- 1 file changed, 329 insertions(+), 3 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..3779106 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,339 @@ "\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": 52, + "id": "d2bf0db2-808a-4404-906d-1f650b8fed95", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "196c2c88-46dc-48de-916f-7f2253943dd6", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of 't-shirt' available in the inventory: 10\n", + "Please enter the quantity of 'mug' available in the inventory: 10\n", + "Please enter the quantity of 'hat' available in the inventory: 10\n", + "Please enter the quantity of 'book' available in the inventory: 10\n", + "Please enter the quantity of 'keychain' available in the inventory: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "UPDATED INVENTORY: {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "# 1.1 - Look at the code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "def ask_quantity(product):\n", + " while True:\n", + " quantity = input(f\"Please enter the quantity of '{product}' available in the inventory: \")\n", + " if quantity.isdigit():\n", + " return int(quantity)\n", + " else:\n", + " print(f\"Invalid input for '{product}'. The quantity must be a whole positive number. Try again.\")\n", + "\n", + "for product in products:\n", + " inventory[product] = ask_quantity(product)\n", + "\n", + "print(\"UPDATED INVENTORY:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "675f0139-77e7-43ac-ac1c-dc40fae09d02", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PRODUCTS LIST: t-shirt, mug, hat, book, keychain\n", + "From the PRODUCTS LIST, please enter the name of three products that a customer wants to order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product nº 1: mug\n", + "Enter product nº 2: hat\n", + "Enter product nº 3: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CUSTOMER ORDER: hat, book, mug\n" + ] + } + ], + "source": [ + "# 1.2 - Look at the code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "customer_order = set()\n", + "\n", + "print(\"PRODUCTS LIST: \", \", \".join(products))\n", + "print(\"From the PRODUCTS LIST, please enter the name of three products that a customer wants to order.\") \n", + "\n", + "while len(customer_order) < 3:\n", + " order = input(f\"Enter product nº {len(customer_order)+1}: \").strip().lower()\n", + " \n", + " if order not in products:\n", + " print(f\"Error: '{order.upper()}' is not in the products list. Try again.\")\n", + " \n", + " elif order in customer_order:\n", + " print(f\"Error: You've already chosen '{order.upper()}'. Pick a different product.\")\n", + "\n", + " else:\n", + " customer_order.add(order)\n", + "\n", + "print(\"CUSTOMER ORDER: \", \", \".join(customer_order))" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "9b321ab0-9277-411b-8534-a854529737fd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PRODUCTS LIST: t-shirt, mug, hat, book, keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "From the products list, enter the name of a product that a customer wants to order: hat\n", + "Do you want to input another order? (yes/no): yes\n", + "From the products list, enter the name of a product that a customer wants to order: book\n", + "Do you want to input another order? (yes/no): yes\n", + "From the products list, enter the name of a product that a customer wants to order: mug\n", + "Do you want to input another order? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CUSTOMER ORDERS: hat, book, mug\n", + "UPDATED INVENTORY: {'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "# 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", + "customer_orders = set()\n", + "\n", + "print(\"PRODUCTS LIST: \", \", \".join(products))\n", + "\n", + "def process_order():\n", + " order = input(\"From the products list, enter the name of a product that a customer wants to order: \").strip().lower()\n", + " \n", + " if order not in products:\n", + " print(f\"Error: '{order.upper()}' is not in the products list. Try again.\")\n", + " return False\n", + " \n", + " elif order in customer_orders:\n", + " print(f\"Error: You've already chosen '{order.upper()}'. Pick a different product.\")\n", + " return False\n", + " \n", + " else:\n", + " customer_orders.add(order)\n", + "\n", + "process_order()\n", + "\n", + "while len(customer_orders) < 5:\n", + " wish_new_order = input(\"Do you want to input another order? (yes/no): \").strip().lower()\n", + " \n", + " if wish_new_order == \"yes\":\n", + " process_order()\n", + " elif wish_new_order == \"no\":\n", + " break\n", + " else:\n", + " print(\"Invalid input: must enter 'yes' or 'no'!\")\n", + "\n", + "print(\"CUSTOMER ORDERS:\", \", \".join(customer_orders))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "ec5e006d-1771-40a2-ab04-1a3b8af3d5f4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PRODUCTS LIST: t-shirt, mug, hat, book, keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "From the products list, enter the name of a product that a customer wants to order: hd\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'HD' is not in the products list. Try again.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to input another order? (yes/no): yes\n", + "From the products list, enter the name of a product that a customer wants to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'HAT' has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to input another order? (yes/no): yes\n", + "From the products list, enter the name of a product that a customer wants to order: boo\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'BOO' is not in the products list. Try again.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to input another order? (yes/no): yes\n", + "From the products list, enter the name of a product that a customer wants to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'BOOK' has been added to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to input another order? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CUSTOMER ORDERS: hat, book\n", + "UPDATED INVENTORY: {'t-shirt': 10, 'mug': 10, 'hat': 9, 'book': 9, 'keychain': 10}\n" + ] + } + ], + "source": [ + "# 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\").\n", + "\n", + "customer_orders = set()\n", + "\n", + "print(\"PRODUCTS LIST: \", \", \".join(products))\n", + "\n", + "def process_order():\n", + " order = input(\"From the products list, enter the name of a product that a customer wants to order: \").strip().lower()\n", + " \n", + " if order not in products:\n", + " print(f\"Error: '{order.upper()}' is not in the products list. Try again.\")\n", + " return False\n", + " \n", + " elif order in customer_orders:\n", + " print(f\"Error: You've already chosen '{order.upper()}'. Pick a different product.\")\n", + " return False\n", + "\n", + " elif inventory[order] <= 0:\n", + " print(f\"Error: '{order.upper()}' is out of stock. Please choose another product.\")\n", + " return False\n", + " \n", + " else:\n", + " customer_orders.add(order)\n", + " inventory[order] -= 1\n", + " print(f\"'{order.upper()}' has been added to your order.\")\n", + "\n", + "process_order()\n", + "\n", + "while len(customer_orders) < 5:\n", + " wish_new_order = input(\"Do you want to input another order? (yes/no): \").strip().lower()\n", + " \n", + " if wish_new_order == \"yes\":\n", + " process_order()\n", + " \n", + " elif wish_new_order == \"no\":\n", + " break\n", + " \n", + " else:\n", + " print(\"Invalid input: must enter 'yes' or 'no'!\")\n", + "\n", + "print(\"CUSTOMER ORDERS:\", \", \".join(customer_orders))\n", + "print(\"UPDATED INVENTORY:\", inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f297a9b-af5b-4173-a037-d266c5612c44", + "metadata": {}, + "outputs": [], + "source": [] } ], "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 +381,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,