From 1843588e3965b3eca1764244b3c34bd18000dd9a Mon Sep 17 00:00:00 2001 From: pietroguido90-design Date: Tue, 15 Sep 2026 16:25:41 +0100 Subject: [PATCH] LAB Flow Control COmpleted --- lab-python-flow-control.ipynb | 118 +++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..ad4d05d 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,125 @@ "\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": 47, + "id": "14e1262c", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "e09a86f1", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "afc8584f", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "48217e17", + "metadata": {}, + "outputs": [], + "source": [ + "for product in products:\n", + " num_str = input(f\"Enter quantity of {product}: \")\n", + " n_items = int(num_str)\n", + " inventory[product] = n_items" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "fdc04818", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "f1b021c3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 2\n", + "mug: -4\n", + "hat: 5\n", + "book: 3\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "while True:\n", + " # 1. Ask the user for a product name inside the loop\n", + " product_name = input(f\"Enter a product to order (Available: {list(inventory.keys())}): \").lower().strip()\n", + " \n", + " # 2. Check if the product exists in inventory\n", + " if product_name not in inventory:\n", + " print(f\"Sorry, '{product_name}' is not in stock.\\n\")\n", + " continue # Re-starts the loop so the user can try typing a valid product\n", + " \n", + " # 3. Add valid product to customer_orders\n", + " customer_orders.add(product_name)\n", + "\n", + " # Check if the product exists in the inventory dictionary\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " \n", + " # 4. Ask if they want to add another item with validation\n", + " while True:\n", + " another = input(\"Do you want to add another product? (yes/no): \").lower().strip()\n", + " if another in [\"yes\", \"no\"]:\n", + " break\n", + " print(\"Invalid answer! Please respond with 'yes' or 'no'.\")\n", + " \n", + " # 5. Stop the main loop if user says 'no'\n", + " if another == \"no\":\n", + " break\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": ".venv (3.14.6.final.0)", "language": "python", "name": "python3" }, @@ -55,7 +169,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,