From 4c7aec38291b8a64f8bca3a040ee95134967fa1f Mon Sep 17 00:00:00 2001 From: Alessia Mattera Date: Sat, 17 Jan 2026 15:21:16 +0100 Subject: [PATCH] Lab2FlowControl --- lab-python-flow-control.ipynb | 135 +++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..dd42429 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,142 @@ "\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": null, + "id": "5e71d92a", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "db661387", + "metadata": {}, + "outputs": [], + "source": [ + "Exercise #2" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "28bb62d1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "products = ['t-shirt','mug', 'hat','book','keychain']\n", + "print(products)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "de9c0e85", + "metadata": {}, + "outputs": [], + "source": [ + "product_names = []\n", + "for product in products:\n", + " product_name = input(f\"Enter the {product} name: \")\n", + " product_names.append(product_name)\n", + " print(\"The product names are:\", product_names)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f418c8a4", + "metadata": {}, + "outputs": [], + "source": [ + "customer_order = []\n", + "for product in products:\n", + " product_name = input(f\"Enter the {product} name: \")\n", + " customer_order.append(product_name)\n", + "print(\"The customer order is:\", customer_order)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "484022ec", + "metadata": {}, + "outputs": [], + "source": [ + "products = []\n", + "\n", + "while True:\n", + " product_name = input(\"Enter the product name: \")\n", + " products.append(product_name)\n", + " add_another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if add_another != \"yes\":\n", + " break\n", + "\n", + "print(\"The products are:\", products)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a05bc2f0", + "metadata": {}, + "outputs": [], + "source": [ + "products = []\n", + "\n", + "add_another = \"yes\" # Set a default value to start the loop\n", + "\n", + "while add_another.lower() == \"yes\":\n", + " product_name = input(\"Enter the product name: \")\n", + " products.append(product_name)\n", + " add_another = input(\"Do you want to add another product? (yes/no): \")\n", + "\n", + "print(\"The products are:\", products)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "47d0bd39", + "metadata": {}, + "outputs": [], + "source": [ + "Excercise #3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61a87f62", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = [\"mug\", \"keychain\", \"hat\"]\n", + "\n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"{product} is not in the inventory.\")\n", + "\n", + "print(\"Updated inventory:\", inventory)\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +186,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,