From 7e21b841be5ddcb02802bef97d1cfaadcfbd23df Mon Sep 17 00:00:00 2001 From: nadiyashahaibi-DA Date: Thu, 23 Jul 2026 19:39:16 +0100 Subject: [PATCH 1/2] Completed lab --- lab-python-flow-control.ipynb | 148 +++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..e0606b7 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,152 @@ "\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": 3, + "id": "4b36a05d-191d-4933-9171-9392e51184d3", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter name of product to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 1 hat to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Add another product? (yes/no): mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products ordered: ['hat']\n", + "update inventory: {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "update_inventory = {product: 5 for product in products}\n", + "\n", + "customer_orders = []\n", + "\n", + "condition = True\n", + "\n", + "while condition:\n", + " product = input(\"Enter name of product to order: \").lower()\n", + " if product not in products:\n", + " print(\"No product available.\")\n", + " else:\n", + " if inventory[product] == 0:\n", + " print(f\"Out of stock.\")\n", + " else:\n", + " customer_orders.append(product)\n", + " inventory[product] -= 1\n", + " print(f\"Added 1 {product} to your order.\")\n", + " \n", + " more = input(\"Add another product? (yes/no): \").lower()\n", + " if more != \"yes\":\n", + " condition = False\n", + "\n", + "print(\"Products ordered: \", customer_orders)\n", + "print(\"update inventory:\", update_inventory)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2cb2a6c5-a277-402a-8c9f-418328ae9097", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "4587f20e-eb71-463b-a6dd-10f47d0b37dc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter name of product to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Added 1 hat to your order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Add another product? (yes/no): hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products ordered: ['hat']\n", + "update inventory: {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "update_inventory = {product: 5 for product in products}\n", + "\n", + "customer_orders = []\n", + "\n", + "while True:\n", + " product = input(\"Enter name of product to order: \").lower()\n", + " \n", + " if product not in products:\n", + " print(\"No product available.\")\n", + " else:\n", + " if inventory[product] == 0:\n", + " print(f\"Out of stock.\")\n", + " else:\n", + " customer_orders.append(product)\n", + " inventory[product] -= 1\n", + " print(f\"Added 1 {product} to your order.\")\n", + " \n", + " more = input(\"Add another product? (yes/no): \").lower()\n", + " if more != \"yes\":\n", + " break\n", + "\n", + "print(\"Products ordered: \", customer_orders)\n", + "print(\"update inventory:\", update_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b0bf5c94-47e4-490f-9ec1-a007faa73a07", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +201,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4, From 6da07a17311a758e81688c62a439c71e7ca71de5 Mon Sep 17 00:00:00 2001 From: nadiyashahaibi-DA Date: Sat, 1 Aug 2026 12:56:23 +0100 Subject: [PATCH 2/2] Lab completed and amended --- lab-python-flow-control.ipynb | 192 ++++++++++++++++++++++------------ 1 file changed, 123 insertions(+), 69 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index e0606b7..a378795 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -40,142 +40,196 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "4b36a05d-191d-4933-9171-9392e51184d3", "metadata": {}, "outputs": [ - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Enter name of product to order: hat\n" - ] - }, { "name": "stdout", "output_type": "stream", "text": [ - "Added 1 hat to your order.\n" + "Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "Enter the quantity for each product:\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Add another product? (yes/no): mug\n" + "Quantity for t-shirt: 2\n", + "Quantity for mug: 3\n", + "Quantity for hat: 2\n", + "Quantity for book: 1\n", + "Quantity for keychain: 2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Products ordered: ['hat']\n", - "update inventory: {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n" + "\n", + "Final inventory:\n", + "{'t-shirt': 2, 'mug': 3, 'hat': 2, 'book': 1, 'keychain': 2}\n" ] } ], "source": [ + "## 1 . Look at your code from the lab data structures, and improve repeated code with loops\n", + "\n", + "# Define a list of products\n", + "\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(\"Available products:\", products)\n", "\n", - "update_inventory = {product: 5 for product in products}\n", + "# Create an empty inventory dictionary\n", + "inventory = {}\n", "\n", - "customer_orders = []\n", + "# Ask the user to input the quantity of each product\n", + "print(\"\\nEnter the quantity for each product:\")\n", "\n", - "condition = True\n", + "for item in products:\n", + " while True: \n", + " quantity = input(f\"Quantity for {item}: \")\n", "\n", - "while condition:\n", - " product = input(\"Enter name of product to order: \").lower()\n", - " if product not in products:\n", - " print(\"No product available.\")\n", - " else:\n", - " if inventory[product] == 0:\n", - " print(f\"Out of stock.\")\n", + " \n", + " if quantity.isdigit():\n", + " inventory[item] = int(quantity)\n", + " break \n", " else:\n", - " customer_orders.append(product)\n", - " inventory[product] -= 1\n", - " print(f\"Added 1 {product} to your order.\")\n", - " \n", - " more = input(\"Add another product? (yes/no): \").lower()\n", - " if more != \"yes\":\n", - " condition = False\n", + " print(\"Invalid input. Please enter a number.\")\n", "\n", - "print(\"Products ordered: \", customer_orders)\n", - "print(\"update inventory:\", update_inventory)\n", - " " + "# Print final inventory\n", + "print(\"\\nFinal inventory:\")\n", + "print(inventory)\n" ] }, { "cell_type": "code", - "execution_count": null, - "id": "2cb2a6c5-a277-402a-8c9f-418328ae9097", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "4587f20e-eb71-463b-a6dd-10f47d0b37dc", + "execution_count": 2, + "id": "c07c3fad-c992-4134-be3a-d4f8212d07e5", "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Add products to the customer's order.\n", + "Type the product name exactly as in the product list.\n", + "\n" + ] + }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter name of product to order: hat\n" + "Enter a product to order: keychain\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Added 1 hat to your order.\n" + "Added 'keychain' to the order.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Add another product? (yes/no): hat\n" + "Do you want to add another product? (yes/no): tshirt\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Products ordered: ['hat']\n", - "update inventory: {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n" + "\n", + "No more products will be added.\n", + "\n", + "Customer orders: {'keychain'}\n" ] } ], "source": [ - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "#2. Prompt the user to enter the name of a product that a customer wants to order.\n", "\n", - "update_inventory = {product: 5 for product in products}\n", + "customer_orders = set()\n", "\n", - "customer_orders = []\n", + "print(\"\\nAdd products to the customer's order.\")\n", + "print(\"Type the product name exactly as in the product list.\\n\")\n", "\n", "while True:\n", - " product = input(\"Enter name of product to order: \").lower()\n", - " \n", - " if product not in products:\n", - " print(\"No product available.\")\n", - " else:\n", - " if inventory[product] == 0:\n", - " print(f\"Out of stock.\")\n", - " else:\n", - " customer_orders.append(product)\n", - " inventory[product] -= 1\n", - " print(f\"Added 1 {product} to your order.\")\n", - " \n", - " more = input(\"Add another product? (yes/no): \").lower()\n", - " if more != \"yes\":\n", - " break\n", + " # Ask user for a product name\n", + " product = input(\"Enter a product to order: \").strip().lower()\n", "\n", - "print(\"Products ordered: \", customer_orders)\n", - "print(\"update inventory:\", update_inventory)" + " # Add product to the set\n", + " customer_orders.add(product)\n", + " print(f\"Added '{product}' to the order.\")\n", + "\n", + " # Ask if they want to add another product\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + "\n", + " # Stop if user says no\n", + " if another != \"yes\":\n", + " print(\"\\nNo more products will be added.\")\n", + " break\n", + "\n", + "# Print final customer order\n", + "print(\"\\nCustomer orders:\", customer_orders)\n" ] }, + { + "cell_type": "code", + "execution_count": 3, + "id": "14b86f01-f242-44ae-a660-d9001a82b69d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updating inventory based on customer orders...\n", + "Reduced inventory for keychain. New quantity: 1\n" + ] + } + ], + "source": [ + "#3. Instead of updating the inventory by subtracting 1 from the quantity of each product, \n", + "#only do it for the products that were ordered (those in \"customer_orders\").\n", + "\n", + "print(\"\\nUpdating inventory based on customer orders...\")\n", + "\n", + "for item in customer_orders:\n", + " # Check if the ordered product exists in the inventory\n", + " if item in inventory:\n", + " if inventory[item] > 0:\n", + " inventory[item] -= 1\n", + " print(f\"Reduced inventory for {item}. New quantity: {inventory[item]}\")\n", + " else:\n", + " print(f\"Cannot reduce inventory for {item}. Stock is already 0.\")\n", + " else:\n", + " print(f\"'{item}' is not a valid product and cannot be updated.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2cb2a6c5-a277-402a-8c9f-418328ae9097", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4587f20e-eb71-463b-a6dd-10f47d0b37dc", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -187,9 +241,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python (myenv)", "language": "python", - "name": "python3" + "name": "myenv" }, "language_info": { "codemirror_mode": {