From bae4c6d88ada90af197bce7f17d2df0855c559b4 Mon Sep 17 00:00:00 2001 From: Phoebe Philipszoon Date: Sat, 25 Jul 2026 18:52:15 +0200 Subject: [PATCH] Solved flow control lab --- lab-python-flow-control.ipynb | 120 +++++++++++++++++++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..fc1ba40 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,124 @@ "\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": "cbc15b21-6733-4520-bd75-b0c5e185ccce", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "838a34b9-e5fb-4353-a6b6-f59bad6566e0", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa254cb8-fe0c-44f6-87cf-7c2e387ce3c0", + "metadata": {}, + "outputs": [], + "source": [ + "for product in products:\n", + " quantity = int(input(f\"How many {product} are in stock? \"))\n", + " inventory[product] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14304f3d-d6c3-468a-85b2-99c7fc85007b", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7be8105c-d975-414e-9b32-8c86376639d9", + "metadata": {}, + "outputs": [], + "source": [ + "product = input(\"Enter a product you want to order: \")\n", + "customer_orders.add(product)\n", + "\n", + "add_another_product = input(\"Do you want to add another product? (yes/no): \")\n", + "\n", + "while add_another_product == \"yes\":\n", + " product = input(\"Enter a product you want to order: \")\n", + " customer_orders.add(product) \n", + " \n", + " add_another_product = input(\"Do you want to add another product? (yes/no): \") " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "251e6dae-1b7c-408d-a6b7-1e2dc418395e", + "metadata": {}, + "outputs": [], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8f62cbf8-ccf1-454e-ae1e-8b3edc058455", + "metadata": {}, + "outputs": [], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (len (customer_orders) / len(products) * 100)\n", + "order_status = (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8766400b-5bda-4183-ad45-65c67822be21", + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total_products_ordered)\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "50a58526-e700-4c74-a184-2d30e3f2f1a7", + "metadata": {}, + "outputs": [], + "source": [ + "for product in customer_orders:\n", + " inventory[product] = inventory[product] - 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa86cf3d-c17c-467a-a482-dea58ff0e3fd", + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Updated Inventory:\")\n", + "for product, quantity in inventory.items(): \n", + " print(product, \":\", quantity) " + ] } ], "metadata": { @@ -55,7 +173,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,