From 935e3cd5b4debcfc275d419e49b0592687c0c8c2 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Thu, 23 Oct 2025 18:46:51 +0200 Subject: [PATCH 1/4] Update lab-python-flow-control.ipynb --- lab-python-flow-control.ipynb | 433 +++++++++++++++++++++++++++++++++- 1 file changed, 430 insertions(+), 3 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..bbc47a3 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -7,7 +7,7 @@ "tags": [] }, "source": [ - "# Lab | Flow Control" + "# Lab | Flow Control | Samia" ] }, { @@ -37,11 +37,438 @@ "\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": "markdown", + "id": "1ac5f241", + "metadata": {}, + "source": [ + "## Lab | Data Structures" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "b5e55f91", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# 1.defining the list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ae4713ce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "# 2. creating an empty dictionary invetory\n", + "inventory = {}\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c5f0b30d", + "metadata": {}, + "outputs": [], + "source": [ + "# 3. input of the user for quantity of each product\n", + "for i in range(len(products)):\n", + " quantity = int(input(\"Enter the quantity for \" + products[i] + \": \"))\n", + " inventory[products[i]] = quantity" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "24fd72c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 10, 'hat': 15, 'book': 20, 'keychain': 25}\n" + ] + } + ], + "source": [ + "# 3.5 checking our dictionary inventory\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9f9c30d6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "# 4. creating an empty set customer orders\n", + "customer_orders = set()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "4d44adad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the list of products:\n", + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "Enter the 3 products to order:\n", + "mug was added to your order.\n", + "book was added to your order.\n", + "hat was added to your order.\n" + ] + } + ], + "source": [ + "# 5. user input for 3 products to order\n", + "print(\"Here is the list of products:\")\n", + "print(products)\n", + "\n", + "print(\"\\nEnter the 3 products to order:\")\n", + "\n", + "for i in range(3):\n", + " item = input(\"Enter a product name: \").lower()\n", + " if item in products:\n", + " customer_orders.add(item)\n", + " print(item + \" was added to your order.\")\n", + " else:\n", + " print(\"Invalid product. Please choose from the list of products.\")\n", + " print(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c7de187a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'mug', 'book', 'hat'}\n" + ] + } + ], + "source": [ + "# 6. Printing the ordered products\n", + "print(\"Customer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "24921c8e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total unique products ordered: 3\n", + "Percentage of products ordered: 60.0 %\n" + ] + } + ], + "source": [ + "# 7+8. Calculating order statistics and Printing order statistics\n", + "print(\"Order Statistics:\")\n", + "# total of products ordered\n", + "total_products_ordered = len(customer_orders)\n", + "print(\"Total unique products ordered:\", total_products_ordered)\n", + "\n", + "# percentage of products ordered\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n", + "\n", + "# Storing order status\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "# Printing order status\n", + "#print(\"Order Status (Total, Percentage):\", order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "a36cfa0e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt : 5\n", + "mug : 9\n", + "hat : 14\n", + "book : 19\n", + "keychain : 25\n" + ] + } + ], + "source": [ + "# 9. Updating the inventory by subtracting 1 for each ordered product\n", + "for ordered_product in customer_orders:\n", + " if inventory[ordered_product] > 0:\n", + " inventory[ordered_product] = inventory[ordered_product] - 1\n", + "\n", + "# 10. Printing the updated inventory\n", + "print(\"Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)" + ] + }, + { + "cell_type": "markdown", + "id": "71fc2ec1", + "metadata": {}, + "source": [ + "## Lab | Flow Control | Samia" + ] + }, + { + "cell_type": "markdown", + "id": "ba9f045d", + "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." + ] + }, + { + "cell_type": "markdown", + "id": "cec3cb4d", + "metadata": {}, + "source": [ + "## 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": 10, + "id": "73a0a88d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "#1. Looking at my code from lab data structures, and improving repeated code with loops:\n", + "# Define the list of products \n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)\n", + "# Create an empty dictionary to store inventory\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "8485511b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product name entered: mug\n" + ] + } + ], + "source": [ + "#2.a. Prompt user to enter the name of a product that a customer wants to order:\n", + "product_name = input(\"Enter the name of the product you want to order: \")\n", + "print(\"Product name entered:\", product_name)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "ae7a7caf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'mug'}\n" + ] + } + ], + "source": [ + "#2.b. Add the product name to the \"customer_orders\" set:\n", + "customer_orders = set()\n", + "customer_orders.add(product_name)\n", + "print(\"Customer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f9f9690e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No more products to add.\n", + "No more products to add.\n" + ] + } + ], + "source": [ + "#2.c. Ask user if they want to add another product (yes/no):\n", + "answer = input(\"Do you want to add another product? (yes/no): \").lower()\n", + "if answer == \"yes\":\n", + " product_name = input(\"Enter the name of the product you want to order: \")\n", + " print(\"Product name entered:\", product_name)\n", + " customer_orders.add(product_name)\n", + "else:\n", + " print(\"No more products to add.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "075b9530", + "metadata": {}, + "outputs": [], + "source": [ + "#### Another Version of 2.c with validation ####\n", + "\n", + "#2.c. Ask user if they want to add another product (yes/no):\n", + "answer = input(\"Do you want to add another product? (yes/no): \").lower()\n", + "if answer == \"yes\":\n", + " product_name = input(\"Enter the name of the product you want to order: \")\n", + " if product_name in products:\n", + " print(\"Product name entered:\", product_name)\n", + " customer_orders.add(product_name)\n", + " else:\n", + " print(\"Invalid product name.\")\n", + "else:\n", + " print(\"No more products to add.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba84b476", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product name entered: book\n", + "Invalid input. Please enter 'yes' or 'no'.\n", + "Product name entered: book\n", + "Invalid input. Please enter 'yes' or 'no'.\n", + "Invalid input. Please enter 'yes' or 'no'.\n", + "Invalid input. Please enter 'yes' or 'no'.\n", + "Invalid input. Please enter 'yes' or 'no'.\n", + "Invalid input. Please enter 'yes' or 'no'.\n", + "No more products to add!\n" + ] + } + ], + "source": [ + "#2.d. Loop until the user decides to stop adding products:\n", + "while True:\n", + " answer = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if answer == \"yes\":\n", + " product_name = input(\"Enter the name of the product you want to order: \")\n", + " print(\"Product name entered:\", product_name)\n", + " customer_orders.add(product_name)\n", + " elif answer == \"no\":\n", + " print(\"No more products to add!\")\n", + " break\n", + " else:\n", + " print(\"Invalid input! Please enter 'yes' or 'no'.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c54d7740", + "metadata": {}, + "outputs": [], + "source": [ + "#3. Update inventory for ordered products only\n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " if inventory[product] == 0:\n", + " del inventory[product]" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +482,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From 046c90f462cc0be67b729a248e0e634c048012e6 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Mon, 3 Nov 2025 23:48:45 +0100 Subject: [PATCH 2/4] commit --- lab-python-flow-control.ipynb | 39 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index bbc47a3..424ed74 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -152,9 +152,9 @@ "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", "\n", "Enter the 3 products to order:\n", + "hat was added to your order.\n", "mug was added to your order.\n", - "book was added to your order.\n", - "hat was added to your order.\n" + "book was added to your order.\n" ] } ], @@ -185,7 +185,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Customer Orders: {'mug', 'book', 'hat'}\n" + "Customer Orders: {'mug', 'hat', 'book'}\n" ] } ], @@ -334,7 +334,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Product name entered: mug\n" + "Product name entered: \n" ] } ], @@ -346,7 +346,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 12, "id": "ae7a7caf", "metadata": {}, "outputs": [ @@ -354,7 +354,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Customer Orders: {'mug'}\n" + "Customer Orders: {''}\n" ] } ], @@ -367,7 +367,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "f9f9690e", "metadata": {}, "outputs": [ @@ -375,7 +375,6 @@ "name": "stdout", "output_type": "stream", "text": [ - "No more products to add.\n", "No more products to add.\n" ] } @@ -393,10 +392,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "075b9530", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No more products to add.\n" + ] + } + ], "source": [ "#### Another Version of 2.c with validation ####\n", "\n", @@ -415,7 +422,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "ba84b476", "metadata": {}, "outputs": [ @@ -423,14 +430,6 @@ "name": "stdout", "output_type": "stream", "text": [ - "Product name entered: book\n", - "Invalid input. Please enter 'yes' or 'no'.\n", - "Product name entered: book\n", - "Invalid input. Please enter 'yes' or 'no'.\n", - "Invalid input. Please enter 'yes' or 'no'.\n", - "Invalid input. Please enter 'yes' or 'no'.\n", - "Invalid input. Please enter 'yes' or 'no'.\n", - "Invalid input. Please enter 'yes' or 'no'.\n", "No more products to add!\n" ] } @@ -452,7 +451,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "c54d7740", "metadata": {}, "outputs": [], From c35fd97a1b2878178e48b100bf4f3d926305b450 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Tue, 4 Nov 2025 01:14:56 +0100 Subject: [PATCH 3/4] commit --- lab-python-flow-control.ipynb | 538 +++++++++++++++++++--------------- 1 file changed, 305 insertions(+), 233 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 424ed74..93ad628 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -7,7 +7,8 @@ "tags": [] }, "source": [ - "# Lab | Flow Control | Samia" + "# Lab 02 | Flow Control\n", + "------" ] }, { @@ -40,221 +41,250 @@ }, { "cell_type": "markdown", - "id": "1ac5f241", + "id": "53cb230b", "metadata": {}, "source": [ - "## Lab | Data Structures" + "## Solved Lab 01 | Data Structures\n", + "----" + ] + }, + { + "cell_type": "markdown", + "id": "58c41def", + "metadata": {}, + "source": [ + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"" ] }, { "cell_type": "code", - "execution_count": 1, - "id": "b5e55f91", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" - ] - } - ], + "execution_count": null, + "id": "a8634fe2", + "metadata": {}, + "outputs": [], "source": [ "# 1.defining the list products\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "print(products)" ] }, + { + "cell_type": "markdown", + "id": "59dc1112", + "metadata": {}, + "source": [ + "2. Create an empty dictionary called `inventory`." + ] + }, { "cell_type": "code", - "execution_count": 2, - "id": "ae4713ce", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{}\n" - ] - } - ], + "execution_count": null, + "id": "b9ab03d6", + "metadata": {}, + "outputs": [], "source": [ "# 2. creating an empty dictionary invetory\n", "inventory = {}\n", "print(inventory)" ] }, + { + "cell_type": "markdown", + "id": "64082237", + "metadata": {}, + "source": [ + "3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values." + ] + }, { "cell_type": "code", - "execution_count": 3, - "id": "c5f0b30d", + "execution_count": null, + "id": "e671b805", "metadata": {}, "outputs": [], "source": [ "# 3. input of the user for quantity of each product\n", "for i in range(len(products)):\n", - " quantity = int(input(\"Enter the quantity for \" + products[i] + \": \"))\n", - " inventory[products[i]] = quantity" + " while True: # keep asking until valid\n", + " quantity = input(\"Enter the quantity for \" + products[i] + \": \")\n", + " if quantity.isdigit(): # checks if input is a positive integer\n", + " quantity = int(quantity)\n", + " inventory[products[i]] = quantity\n", + " break # valid → exit loop\n", + " else:\n", + " print(f\"❌ Invalid input! Please enter a numeric quantity for {products[i]}.\")\n", + "\n", + "# 3.5 checking our dictionary inventory\n", + "print(inventory)" ] }, { - "cell_type": "code", - "execution_count": 4, - "id": "24fd72c4", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'t-shirt': 5, 'mug': 10, 'hat': 15, 'book': 20, 'keychain': 25}\n" - ] - } - ], + "cell_type": "markdown", + "id": "17d93e50", + "metadata": {}, "source": [ - "# 3.5 checking our dictionary inventory\n", - "print(inventory)" + "4. Create an empty set called `customer_orders`." ] }, { "cell_type": "code", - "execution_count": 5, - "id": "9f9c30d6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "set()\n" - ] - } - ], + "execution_count": null, + "id": "d394e871", + "metadata": {}, + "outputs": [], "source": [ "# 4. creating an empty set customer orders\n", "customer_orders = set()\n", "print(customer_orders)" ] }, + { + "cell_type": "markdown", + "id": "0052d35a", + "metadata": {}, + "source": [ + "5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the `customer_orders` set." + ] + }, { "cell_type": "code", - "execution_count": 6, - "id": "4d44adad", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Here is the list of products:\n", - "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "\n", - "Enter the 3 products to order:\n", - "hat was added to your order.\n", - "mug was added to your order.\n", - "book was added to your order.\n" - ] - } - ], - "source": [ - "# 5. user input for 3 products to order\n", + "execution_count": null, + "id": "812d4117", + "metadata": {}, + "outputs": [], + "source": [ + "# 5. User input for 3 products to order\n", "print(\"Here is the list of products:\")\n", "print(products)\n", "\n", - "print(\"\\nEnter the 3 products to order:\")\n", + "print(\"Enter the names of 3 products to order:\")\n", "\n", - "for i in range(3):\n", + "while len(customer_orders) < 3:\n", " item = input(\"Enter a product name: \").lower()\n", + "\n", " if item in products:\n", " customer_orders.add(item)\n", - " print(item + \" was added to your order.\")\n", + " print(f\"✅ Added '{item}' to customer orders.\")\n", " else:\n", - " print(\"Invalid product. Please choose from the list of products.\")\n", - " print(products)" + " print(\"❌ Invalid product! Please choose from:\", products)" + ] + }, + { + "cell_type": "markdown", + "id": "ba960ce5", + "metadata": {}, + "source": [ + "6. Print the products in the `customer_orders` set." ] }, { "cell_type": "code", - "execution_count": 7, - "id": "c7de187a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Customer Orders: {'mug', 'hat', 'book'}\n" - ] - } - ], + "execution_count": null, + "id": "09802680", + "metadata": {}, + "outputs": [], "source": [ "# 6. Printing the ordered products\n", "print(\"Customer Orders:\", customer_orders)" ] }, + { + "cell_type": "markdown", + "id": "839e06ef", + "metadata": {}, + "source": [ + "7. Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + " \n", + " Store these statistics in a tuple called `order_status`." + ] + }, { "cell_type": "code", - "execution_count": 8, - "id": "24921c8e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Order Statistics:\n", - "Total unique products ordered: 3\n", - "Percentage of products ordered: 60.0 %\n" - ] - } - ], - "source": [ - "# 7+8. Calculating order statistics and Printing order statistics\n", - "print(\"Order Statistics:\")\n", - "# total of products ordered\n", - "total_products_ordered = len(customer_orders)\n", - "print(\"Total unique products ordered:\", total_products_ordered)\n", + "execution_count": null, + "id": "6701d3bb", + "metadata": {}, + "outputs": [], + "source": [ + "# 7. Calculating order statistics\n", "\n", - "# percentage of products ordered\n", - "percentage_ordered = (total_products_ordered / len(products)) * 100\n", - "print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n", + "# Total Products Ordered\n", + "total_products_ordered = len(customer_orders)\n", "\n", - "# Storing order status\n", - "order_status = (total_products_ordered, percentage_ordered)\n", + "# Percentage of Products Ordered\n", + "percentage_products_ordered = (total_products_ordered / len(products)) * 100\n", "\n", - "# Printing order status\n", + "# Calculate Order Status\n", + "order_status = (total_products_ordered, percentage_products_ordered)\n", + "# Print Order Status\n", "#print(\"Order Status (Total, Percentage):\", order_status)" ] }, + { + "cell_type": "markdown", + "id": "9f90868e", + "metadata": {}, + "source": [ + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6afe8e7c", + "metadata": {}, + "outputs": [], + "source": [ + "# 8. Storing the statistics in a tuple order_status\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total_products_ordered)\n", + "print(f\"Percentage of Products Ordered: {int(percentage_products_ordered)}%\")" + ] + }, + { + "cell_type": "markdown", + "id": "c832a5ab", + "metadata": {}, + "source": [ + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly." + ] + }, { "cell_type": "code", - "execution_count": 9, - "id": "a36cfa0e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Updated Inventory:\n", - "t-shirt : 5\n", - "mug : 9\n", - "hat : 14\n", - "book : 19\n", - "keychain : 25\n" - ] - } - ], + "execution_count": null, + "id": "ef0a1e9b", + "metadata": {}, + "outputs": [], "source": [ "# 9. Updating the inventory by subtracting 1 for each ordered product\n", "for ordered_product in customer_orders:\n", " if inventory[ordered_product] > 0:\n", - " inventory[ordered_product] = inventory[ordered_product] - 1\n", - "\n", + " inventory[ordered_product] = inventory[ordered_product] - 1" + ] + }, + { + "cell_type": "markdown", + "id": "1705389c", + "metadata": {}, + "source": [ + "10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e29f9b9a", + "metadata": {}, + "outputs": [], + "source": [ "# 10. Printing the updated inventory\n", - "print(\"Updated Inventory:\")\n", + "print(\"Updated Inventory :\")\n", "for product, quantity in inventory.items():\n", " print(product, \":\", quantity)" ] @@ -264,7 +294,8 @@ "id": "71fc2ec1", "metadata": {}, "source": [ - "## Lab | Flow Control | Samia" + "## Solved Lab 02 | Flow Control\n", + "-----" ] }, { @@ -284,7 +315,7 @@ "id": "cec3cb4d", "metadata": {}, "source": [ - "## Follow the steps below to complete the exercise:\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", @@ -298,23 +329,25 @@ " \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\")." + "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\").\n", + "---------" + ] + }, + { + "cell_type": "markdown", + "id": "d9adb3f0", + "metadata": {}, + "source": [ + "\n", + " 1. Look at your code from the lab data structures, and improve repeated code with loops." ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "73a0a88d", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" - ] - } - ], + "outputs": [], "source": [ "#1. Looking at my code from lab data structures, and improving repeated code with loops:\n", "# Define the list of products \n", @@ -324,40 +357,82 @@ "inventory = {}" ] }, + { + "cell_type": "markdown", + "id": "2060afb5", + "metadata": {}, + "source": [ + "------\n", + " 2. Instead of asking the user to input the name of three products that a customer wants to order, \n", + " do the following:" + ] + }, + { + "cell_type": "markdown", + "id": "832d175c", + "metadata": {}, + "source": [ + " 2.a. Prompt the user to enter the name of a product that a customer wants to order." + ] + }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, + "id": "323ed1d8", + "metadata": {}, + "outputs": [], + "source": [ + "# Define the list of products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(\"Available products:\", products)\n", + "\n", + "# Create an empty dictionary to store inventory\n", + "inventory = {}\n", + "\n", + "while True:\n", + " product_name = input(\"Enter the name of the product you want to order: \").lower().strip()\n", + " if product_name in products:\n", + " # Add product to inventory or increase quantity\n", + " if product_name in inventory:\n", + " inventory[product_name] += 1\n", + " else:\n", + " inventory[product_name] = 1\n", + "\n", + " print(f\"You have added the '{product_name}' to inventory.\")\n", + " break # exit loop after successful entry\n", + " else:\n", + " print(\"Product not found. Please choose from:\", products)\n", + "\n", + "print(\"\\nFinal inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "8485511b", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Product name entered: \n" - ] - } - ], + "outputs": [], "source": [ "#2.a. Prompt user to enter the name of a product that a customer wants to order:\n", - "product_name = input(\"Enter the name of the product you want to order: \")\n", - "print(\"Product name entered:\", product_name)" + "print(f\"Available porducts:{products}\")\n", + "product_name = input(\"Enter the name of the product you want to order: \").lower()\n", + "print(\"Product added to order:\", product_name)" + ] + }, + { + "cell_type": "markdown", + "id": "adbc0f3d", + "metadata": {}, + "source": [ + " 2.b. Add the product name to the \"customer_orders\" set." ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "ae7a7caf", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Customer Orders: {''}\n" - ] - } - ], + "outputs": [], "source": [ "#2.b. Add the product name to the \"customer_orders\" set:\n", "customer_orders = set()\n", @@ -366,98 +441,95 @@ ] }, { - "cell_type": "code", - "execution_count": 13, - "id": "f9f9690e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "No more products to add.\n" - ] - } - ], - "source": [ - "#2.c. Ask user if they want to add another product (yes/no):\n", - "answer = input(\"Do you want to add another product? (yes/no): \").lower()\n", - "if answer == \"yes\":\n", - " product_name = input(\"Enter the name of the product you want to order: \")\n", - " print(\"Product name entered:\", product_name)\n", - " customer_orders.add(product_name)\n", - "else:\n", - " print(\"No more products to add.\")" + "cell_type": "markdown", + "id": "8e7e7ad2", + "metadata": {}, + "source": [ + " 2.c. Ask the user if they want to add another product (yes/no)." ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "075b9530", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "No more products to add.\n" - ] - } - ], - "source": [ - "#### Another Version of 2.c with validation ####\n", - "\n", - "#2.c. Ask user if they want to add another product (yes/no):\n", + "outputs": [], + "source": [ + "#2.c. Ask user if they want to add another product (YES/NO):\n", "answer = input(\"Do you want to add another product? (yes/no): \").lower()\n", + "\n", "if answer == \"yes\":\n", - " product_name = input(\"Enter the name of the product you want to order: \")\n", + " print(f\"Your answer was: {answer}\")\n", + " product_name = input(\"Enter the name of the product you want to order: \").lower()\n", " if product_name in products:\n", - " print(\"Product name entered:\", product_name)\n", + " print(\"Product added to order:\", product_name)\n", " customer_orders.add(product_name)\n", " else:\n", " print(\"Invalid product name.\")\n", + "\n", + "elif answer == 'no':\n", + " print(f\"Your answer was: {answer}\")\n", + " print(\"No more products to add.\")\n", + "\n", "else:\n", - " print(\"No more products to add.\")" + " print(f\"Your answer was: {answer}\")\n", + " print(\"Error, Please answer with YES or NO!\")" + ] + }, + { + "cell_type": "markdown", + "id": "527b01bf", + "metadata": {}, + "source": [ + " 2.d. Continue the loop until the user does not want to add another product." ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "ba84b476", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "No more products to add!\n" - ] - } - ], + "outputs": [], "source": [ "#2.d. Loop until the user decides to stop adding products:\n", "while True:\n", " answer = input(\"Do you want to add another product? (yes/no): \").lower()\n", + "\n", " if answer == \"yes\":\n", - " product_name = input(\"Enter the name of the product you want to order: \")\n", - " print(\"Product name entered:\", product_name)\n", - " customer_orders.add(product_name)\n", + " product_name = input(\"Enter the name of the product you want to order: \").lower()\n", + "\n", + " if product_name in products:\n", + " print(\"Product added to order:\", product_name)\n", + " customer_orders.add(product_name)\n", + " else:\n", + " print(\"Invalid product name!\")\n", + "\n", " elif answer == \"no\":\n", - " print(\"No more products to add!\")\n", - " break\n", + " print(\"No more products to add ~\")\n", + " break # <-- stop the loop\n", + "\n", " else:\n", - " print(\"Invalid input! Please enter 'yes' or 'no'.\")" + " print(\"Error, please answer with YES or NO!\")" + ] + }, + { + "cell_type": "markdown", + "id": "47479c66", + "metadata": {}, + "source": [ + "------\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": 16, + "execution_count": null, "id": "c54d7740", "metadata": {}, "outputs": [], "source": [ "#3. Update inventory for ordered products only\n", - "for product in customer_orders:\n", + "for product in list(customer_orders):\n", " if product in inventory:\n", " inventory[product] -= 1\n", " if inventory[product] == 0:\n", From b5b7862618f1672b13b69c5763070deeca6567ba Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Wed, 5 Nov 2025 16:47:10 +0100 Subject: [PATCH 4/4] commit --- lab-python-flow-control.ipynb | 42 +++-------------------------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 93ad628..4ffdb47 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -44,7 +44,7 @@ "id": "53cb230b", "metadata": {}, "source": [ - "## Solved Lab 01 | Data Structures\n", + "# Solved Lab 01 | Data Structures\n", "----" ] }, @@ -294,45 +294,10 @@ "id": "71fc2ec1", "metadata": {}, "source": [ - "## Solved Lab 02 | Flow Control\n", + "# Solved Lab 02 | Flow Control\n", "-----" ] }, - { - "cell_type": "markdown", - "id": "ba9f045d", - "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." - ] - }, - { - "cell_type": "markdown", - "id": "cec3cb4d", - "metadata": {}, - "source": [ - "### 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\").\n", - "---------" - ] - }, { "cell_type": "markdown", "id": "d9adb3f0", @@ -363,8 +328,7 @@ "metadata": {}, "source": [ "------\n", - " 2. Instead of asking the user to input the name of three products that a customer wants to order, \n", - " do the following:" + " 2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:" ] }, {