From 9868236950c9cfad61e350f71b1e6319b5c9d101 Mon Sep 17 00:00:00 2001 From: ceciliabetek Date: Sun, 26 Oct 2025 14:35:22 +0100 Subject: [PATCH 1/2] Update lab-python-flow-control.ipynb --- lab-python-flow-control.ipynb | 103 +++++++++++++--------------------- 1 file changed, 40 insertions(+), 63 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..d68f7bb 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -1,63 +1,40 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", - "metadata": { - "tags": [] - }, - "source": [ - "# Lab | Flow Control" - ] - }, - { - "cell_type": "markdown", - "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", - "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\")." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} +products = ["t-shirt", "mug", "hat", "book", "keychain"] + +inventory = {} + +for product in products: + print ("Enter the quantity of each product available in the inventory for " + product + " : ") + quantity = int(input()) + inventory[product] = quantity + +customer_orders = set() + +while True: + order = input("Enter product ordered: ").lower() + + if order in products: # On vérifie que le produit existe + customer_orders.add(order) + else: + print("This product is not available in the store.") + + more = input("Do you want to add another product? (yes/no): ").lower() + if more != "yes": + break + +print("Products ordered:", customer_orders) + +total_products_ordered = len(customer_orders) +percentage_ordered = (total_products_ordered / len(products)) * 100 +order_status = (total_products_ordered, percentage_ordered) + +print("\nOrder Statistics:") +print("Total Products Ordered:", order_status[0]) +print("Percentage of Products Ordered:", round(order_status[1], 2), "%") + +for item in customer_orders: + if item in inventory: + inventory[item] -= 1 + +print("\nUpdated Inventory:") +for product, quantity in inventory.items(): + print(product, ":", quantity) From 7eff423abc14dd5b684d639251ba8ee316b2c056 Mon Sep 17 00:00:00 2001 From: ceciliabetek Date: Sat, 15 Nov 2025 16:35:50 +0100 Subject: [PATCH 2/2] commit --- flow-control-python.ipynb | 74 +++++++++++++++++++++++++++++++++++ lab-python-flow-control.ipynb | 40 ------------------- 2 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 flow-control-python.ipynb delete mode 100644 lab-python-flow-control.ipynb diff --git a/flow-control-python.ipynb b/flow-control-python.ipynb new file mode 100644 index 0000000..9aade11 --- /dev/null +++ b/flow-control-python.ipynb @@ -0,0 +1,74 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " print (\"Enter the quantity of each product available in the inventory for \" + product + \" : \")\n", + " quantity = int(input())\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "while True:\n", + " order = input(\"Enter product ordered: \").lower()\n", + " \n", + " if order in products: # On vérifie que le produit existe\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"This product is not available in the store.\")\n", + "\n", + " more = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if more != \"yes\":\n", + " break\n", + "\n", + "print(\"Products ordered:\", customer_orders)\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print(\"\\nOrder Statistics:\")\n", + "print(\"Total Products Ordered:\", order_status[0])\n", + "print(\"Percentage of Products Ordered:\", round(order_status[1], 2), \"%\")\n", + "\n", + "for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1\n", + "\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb deleted file mode 100644 index d68f7bb..0000000 --- a/lab-python-flow-control.ipynb +++ /dev/null @@ -1,40 +0,0 @@ -products = ["t-shirt", "mug", "hat", "book", "keychain"] - -inventory = {} - -for product in products: - print ("Enter the quantity of each product available in the inventory for " + product + " : ") - quantity = int(input()) - inventory[product] = quantity - -customer_orders = set() - -while True: - order = input("Enter product ordered: ").lower() - - if order in products: # On vérifie que le produit existe - customer_orders.add(order) - else: - print("This product is not available in the store.") - - more = input("Do you want to add another product? (yes/no): ").lower() - if more != "yes": - break - -print("Products ordered:", customer_orders) - -total_products_ordered = len(customer_orders) -percentage_ordered = (total_products_ordered / len(products)) * 100 -order_status = (total_products_ordered, percentage_ordered) - -print("\nOrder Statistics:") -print("Total Products Ordered:", order_status[0]) -print("Percentage of Products Ordered:", round(order_status[1], 2), "%") - -for item in customer_orders: - if item in inventory: - inventory[item] -= 1 - -print("\nUpdated Inventory:") -for product, quantity in inventory.items(): - print(product, ":", quantity)