From 8f72d2f3e99d5b37ec2e88445089093f1b3bbb32 Mon Sep 17 00:00:00 2001 From: Ana Preto Date: Thu, 6 Aug 2026 18:54:24 +0100 Subject: [PATCH 1/2] Complete flow control exercise --- lab-python-flow-control.ipynb | 81 ++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..f2f1fde 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,88 @@ "\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": 3, + "id": "5990ea77", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product not available. Please choose from the list.\n", + "{'hat', 'mug'}\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.00%\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 9\n", + "hat: 9\n", + "book: 10\n", + "keychain: 10\n" + ] + } + ], + "source": [ + "# 1. Products list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# 2. Empty inventory dictionary\n", + "inventory = {}\n", + "\n", + "# 3. Ask inventory quantities using a loop\n", + "for product in products:\n", + " quantity = int(input(f\"How many {product}s are available? \"))\n", + " inventory[product] = quantity\n", + "\n", + "# 4. Empty customer orders set\n", + "customer_orders = set()\n", + "\n", + "# 5. Ask customer orders using a while loop\n", + "while True:\n", + " product = input(\"Enter a product to order: \")\n", + "\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"Product not available. Please choose from the list.\")\n", + "\n", + " answer = input(\"Do you want to add another product? (yes/no): \")\n", + "\n", + " if answer.lower() == \"no\":\n", + " break\n", + "\n", + "# 6. Print customer orders\n", + "print(customer_orders)\n", + "\n", + "# 7. Calculate statistics\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "# 8. Print statistics\n", + "print(\"Order Statistics:\")\n", + "print(f\"Total Products Ordered: {order_status[0]}\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n", + "\n", + "# 9. Update inventory only for ordered products\n", + "for product in customer_orders:\n", + " inventory[product] -= 1\n", + "\n", + "# 10. Print updated inventory\n", + "print(\"Updated Inventory:\")\n", + "for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +132,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From 57c32769283ad804632a196671f8fb5755db99ab Mon Sep 17 00:00:00 2001 From: Ana Preto Date: Fri, 7 Aug 2026 19:14:42 +0100 Subject: [PATCH 2/2] Complete flow control exercise --- lab-python-flow-control.ipynb | 46 +++++++++++++++++------------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f2f1fde..cb1c52d 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "5990ea77", "metadata": {}, "outputs": [ @@ -48,68 +48,68 @@ "name": "stdout", "output_type": "stream", "text": [ - "Product not available. Please choose from the list.\n", - "{'hat', 'mug'}\n", + "Product not available. Try again.\n", + "{'book', 'hat'}\n", "Order Statistics:\n", "Total Products Ordered: 2\n", - "Percentage of Products Ordered: 40.00%\n", + "Percentage of Products Ordered: 40.0%\n", "Updated Inventory:\n", "t-shirt: 10\n", - "mug: 9\n", + "mug: 10\n", "hat: 9\n", - "book: 10\n", + "book: 9\n", "keychain: 10\n" ] } ], "source": [ - "# 1. Products list\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", - "# 2. Empty inventory dictionary\n", "inventory = {}\n", "\n", - "# 3. Ask inventory quantities using a loop\n", + "# Ask inventory quantities using a loop\n", "for product in products:\n", " quantity = int(input(f\"How many {product}s are available? \"))\n", " inventory[product] = quantity\n", "\n", - "# 4. Empty customer orders set\n", "customer_orders = set()\n", "\n", - "# 5. Ask customer orders using a while loop\n", + "# Ask customer orders using a while loop\n", "while True:\n", - " product = input(\"Enter a product to order: \")\n", "\n", - " if product in products:\n", - " customer_orders.add(product)\n", - " else:\n", - " print(\"Product not available. Please choose from the list.\")\n", + " while True:\n", + " product = input(\"Enter a product to order: \").lower()\n", + "\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " break\n", + " else:\n", + " print(\"Product not available. Try again.\")\n", "\n", " answer = input(\"Do you want to add another product? (yes/no): \")\n", "\n", " if answer.lower() == \"no\":\n", " break\n", "\n", - "# 6. Print customer orders\n", "print(customer_orders)\n", "\n", - "# 7. Calculate statistics\n", + "# Calculate statistics\n", "total_products_ordered = len(customer_orders)\n", "percentage_ordered = (total_products_ordered / len(products)) * 100\n", "\n", "order_status = (total_products_ordered, percentage_ordered)\n", "\n", - "# 8. Print statistics\n", + "# Print statistics\n", "print(\"Order Statistics:\")\n", "print(f\"Total Products Ordered: {order_status[0]}\")\n", - "print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n", + "print(f\"Percentage of Products Ordered: {order_status[1]}%\")\n", "\n", - "# 9. Update inventory only for ordered products\n", + "# Update inventory only for ordered products\n", "for product in customer_orders:\n", - " inventory[product] -= 1\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", "\n", - "# 10. Print updated inventory\n", + "# Print updated inventory\n", "print(\"Updated Inventory:\")\n", "for product in inventory:\n", " print(f\"{product}: {inventory[product]}\")"