From f1e3dd099281fd4022fd7126efa0a395f489a3a8 Mon Sep 17 00:00:00 2001 From: gertxani2-cmyk Date: Sat, 19 Sep 2026 15:42:15 +0200 Subject: [PATCH] Add files via upload --- lab-python-flow-control.ipynb | 63 ++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..6d0dea8 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,67 @@ "\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": "7e7b83a8-e8bc-4e12-8854-d642bd7178f0", + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Define list of products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# 2. Create empty dictionary for inventory\n", + "inventory = {}\n", + "\n", + "# 3. Get quantity for each product from user input\n", + "print(\"--- Enter Inventory Quantities ---\")\n", + "for product in products:\n", + " quantity = int(input(f\"Enter quantity for '{product}': \"))\n", + " inventory[product] = quantity\n", + "\n", + "# 4. Create empty set for customer orders\n", + "customer_orders = set()\n", + "\n", + "# 5. Get product orders dynamically from the user until they want to stop\n", + "print(\"\\n--- Enter Customer Orders ---\")\n", + "user_choice = \"yes\"\n", + "\n", + "while user_choice == \"yes\":\n", + " # a. Prompt the user to enter the name of a product\n", + " order = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " \n", + " # b. Add the product name to the \"customer_orders\" set\n", + " customer_orders.add(order)\n", + " \n", + " # c. Ask the user if they want to add another product (yes/no)\n", + " user_choice = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + "\n", + "# 6. Print the products in customer_orders\n", + "print(\"\\nCustomer Orders:\")\n", + "print(customer_orders)\n", + "\n", + "# 7. Calculate order statistics and store in a tuple\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", + "# 8. Print order statistics in the required format\n", + "print(\"\\nOrder Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.1f}%\")\n", + "\n", + "# 9. Update the inventory by subtracting 1 for ordered items\n", + "for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + "\n", + "# 10. Print updated inventory on separate lines\n", + "print(\"\\nUpdated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] } ], "metadata": { @@ -55,7 +116,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,