From b5239076d538600dbb93c0e0740d87066e1cac21 Mon Sep 17 00:00:00 2001 From: REYNOLD TAKURA CHORUMA <153188514+Bhova45@users.noreply.github.com> Date: Wed, 22 Oct 2025 17:29:46 +0200 Subject: [PATCH 1/2] Flow Control lab done --- lab-python-flow-control.ipynb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..7181dee 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,18 @@ "\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": "fd61404b", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [] } ], "metadata": { From 4af9589fdd52c6a5245e70b726cc1c483a21d48a Mon Sep 17 00:00:00 2001 From: REYNOLD TAKURA CHORUMA <153188514+Bhova45@users.noreply.github.com> Date: Wed, 22 Oct 2025 17:32:09 +0200 Subject: [PATCH 2/2] flow control lab done --- lab-python-flow-control.ipynb | 227 ++++++++++++++++++++++++++++++++-- 1 file changed, 219 insertions(+), 8 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 7181dee..2d528c4 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -41,19 +41,230 @@ { "cell_type": "code", "execution_count": null, - "id": "fd61404b", - "metadata": { - "vscode": { - "languageId": "plaintext" - } - }, + "id": "0a8d7be5", + "metadata": {}, "outputs": [], "source": [] + }, + { + "cell_type": "markdown", + "id": "3f75e995", + "metadata": {}, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "2dcb4c89", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "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", + "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": "abe51832", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--- Inventory Setup ---\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "customer_orders = set()\n", + "\n", + "# The loop to set up the initial inventory:\n", + "print(\"--- Inventory Setup ---\")\n", + "for product in products:\n", + " while True:\n", + " try:\n", + " # Use f-string for clear input prompt\n", + " quantity = int(input(f\"Enter the initial quantity for {product}: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " break\n", + " else:\n", + " print(\"Quantity cannot be negative. Please try again.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31011e93", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Customer Order ---\n", + "Addedt-shirtto the order.\n", + "Addedmugto the order.\n", + "Addedhatto the order.\n", + "Addedt-shirtto the order.\n", + "Addedmugto the order.\n", + "Addedhatto the order.\n", + "Addedhatto the order.\n", + "Addedhatto the order.\n", + "Addedt-shirtto the order.\n", + "Addedt-shirtto the order.\n", + "Addedhatto the order.\n", + "Addedmugto the order.\n", + "Addedmugto the order.\n", + "Addedt-shirtto the order.\n", + "Addedt-shirtto the order.\n", + "Addedt-shirtto the order.\n", + "Addedmugto the order.\n", + "Addedhatto the order.\n", + "Addedmugto the order.\n", + "Addeddone is not valid product.Please choose from the list or type 'done'.\n", + "invalid input. Please enter 'yes' or 'no'.\n" + ] + } + ], + "source": [ + "# Customer Order Input \n", + "print(\"\\n--- Customer Order ---\")\n", + "Ordering = True\n", + "while Ordering:\n", + " # Prompt the user to enter the name of a product \n", + " product = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " product_name = input(f\"Enter a product to order ({','.join(product)})or 'done':\").lower().strip()\n", + " if product_name == 'done':\n", + " Ordering = False\n", + " # Input validation \n", + " if product_name in products: \n", + " # Add the product name to the \"\"customer_orders \"set\n", + " customer_orders.add(product_name)\n", + " print(f\"Added{product_name}to the order.\")\n", + " else:\n", + " print(f\"Added{product_name} is not valid product.Please choose from the list or type 'done'.\")\n", + " # Ask the user if they want to add another product (yes/no)\n", + " while True:\n", + " more =input(\"Do you want to add another product? (yes/no):lower().strip()\")\n", + " if more in ['no', 'n']:\n", + " Ordering = False\n", + " break \n", + " elif more in ['yes','y']:\n", + " break\n", + " else:\n", + " print(\"invalid input. Please enter 'yes' or 'no'.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "676b563d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Products in Customer Orders set:\n", + "{'t-shirt', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# Print the products in the customer_orders set \n", + "from hashlib import new\n", + "\n", + "\n", + "print(\"\\nProducts in Customer Orders set:\")\n", + "print (customer_orders)\n", + "## Calculate and Print Order Statistics \n", + "total_products_available = len(products)\n", + "total_products_ordered = len(customer_orders)\n", + "if total_products_available >0:\n", + " percentage_ordered = (total_products_ordered/total_products_available)* 100\n", + " percentage_ordered = round(percentage_ordered, 2)\n", + "else:\n", + " percentage_ordered = 0.0\n", + " order_status = (total_products_ordered, percentage_ordered)\n", + " print(\"\\nOrder Statistic\")\n", + " print(f\"Total Products Ordered:{order_status[0]}\")\n", + " print(f\"Percentage of Products Ordered:{order_status[1]}%\")\n", + " ## Update inventory for only Ordered Products \n", + " print(\"\\n--- Updating Inventory---\")\n", + " for product_ordered in customer_orders:\n", + " if product_ordered in inventory[product_ordered]>0:\n", + " inventory[product_ordered]-=1\n", + " print(f\"Inventory reduced for {product_ordered}.New quantity:{inventory[product_ordered]}\")\n", + " elif product_ordered in inventory and inventory[product_ordered]==0:\n", + " print(f\"Warning: Attempted to order{product_ordered},but inventory was already 0. Inventory remains 0.\")\n", + " ## Print Updated Inventory \n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}:{quantity}\")\n", + " \n", + "\n", + "\n", + "print(\"\\nProducts in Customer Orders set:\")\n", + "print (customer_orders)\n", + "## Calculate and Print Order Statistics \n", + "total_products_available = len(products)\n", + "total_products_ordered = len(customer_orders)\n", + "if total_products_available >0:\n", + " percentage_ordered = (total_products_ordered/total_products_available)* 100\n", + " percentage_ordered = round(percentage_ordered, 2)\n", + "else:\n", + " percentage_ordered = 0.0\n", + " order_status = (total_products_ordered, percentage_ordered)\n", + " print(\"\\nOrder Statistic\")\n", + " print(f\"Total Products Ordered:{order_status[0]}\")\n", + " print(f\"Percentage of Products Ordered:{order_status[1]}%\")\n", + " ## Update inventory for only Ordered Products \n", + " print(\"\\n--- Updating Inventory---\")\n", + " for product_ordered in customer_orders:\n", + " if product_ordered in inventory[product_ordered]>0:\n", + " inventory[product_ordered]-=1\n", + " print(f\"Inventory reduced for {product_ordered}.New quantity:{inventory[product_ordered]}\")\n", + " elif product_ordered in inventory and inventory[product_ordered]==0:\n", + " print(f\"Warning: Attempted to order{product_ordered},but inventory was already 0. Inventory remains 0.\")\n", + " ## Print Updated Inventory \n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}:{quantity}\")\n", + " " + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -67,7 +278,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,