From 0b5a67b5033a9e97e84fb054b267e49ad9a3aacc Mon Sep 17 00:00:00 2001 From: Aline Granells Date: Tue, 16 Jun 2026 17:10:58 +0100 Subject: [PATCH] Solved lab - Day 2 --- lab-python-flow-control.ipynb | 82 ++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..b55f67f 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,89 @@ "\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": 15, + "id": "ca410860", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer_orders: {'mug', 'book'}\n", + "Order Statistics:\n", + " Total Products Ordered: 2\n", + " Percentage of Products Ordered: 40.00%\n", + "Updated Inventory: \n", + " t-shirt: 5\n", + " mug: 4\n", + " hat: 5\n", + " book: 4\n", + " keychain: 5\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "# Day 1 Lab code updated\n", + "for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "# Doing a looping to prompt the user if they want to another product\n", + "\n", + "add_another = \"yes\"\n", + "\n", + "while add_another == \"yes\":\n", + " # ask for product\n", + " order = input(\"Please input the name of the product: \")\n", + " # add to set\n", + " customer_orders.add(order)\n", + " # ask if user wants another product\n", + " add_another = input(\"Do you want another product? (yes/no)\").lower()\n", + " customer_orders.add(order)\n", + "\n", + "print(\"Customer_orders: \", customer_orders)\n", + "\n", + "# Calculate the inventory\n", + "\n", + "total_order = len(customer_orders)\n", + "\n", + "rate = total_order / len(products)\n", + "\n", + "order_status = (total_order, rate)\n", + "\n", + "for product in customer_orders:\n", + " inventory[product] -= 1\n", + "\n", + "print(f\"\"\"Order Statistics:\n", + " Total Products Ordered: {total_order}\n", + " Percentage of Products Ordered: {rate:0.2%}\"\"\")\n", + "\n", + "print(\"Updated Inventory: \")\n", + "\n", + "for product, quantity in inventory.items():\n", + " print(f\" {product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b2c9650", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -55,7 +133,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.5" } }, "nbformat": 4,