From eccb20df048dffed59c90e03265b9fb248723b47 Mon Sep 17 00:00:00 2001 From: Bruno Sousa <“brunomsousa2014@gmail.com”> Date: Wed, 18 Feb 2026 14:14:47 +0000 Subject: [PATCH] 2nd lab added --- lab-python-flow-control.ipynb | 250 +++++++++++++++++++++++++++++++++- 1 file changed, 249 insertions(+), 1 deletion(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..dd476f8 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,254 @@ "\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": 27, + "id": "b5ff3c0f-a709-4dcd-9d2c-152dc1a8b364", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantify t-shirt product: 10\n", + "Quantify mug product: 50\n", + "Quantify hat product: 20\n", + "Quantify book product: 50\n", + "Quantify keychain product: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 50, 'hat': 20, 'book': 50, 'keychain': 20}\n" + ] + } + ], + "source": [ + "#1\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory ={\"t-shirt\": 0, \"mug\": 0, \"hat\": 0, \"book\": 0, \"keychain\": 0}\n", + "\n", + "for product in inventory:\n", + " inventory[product] = int(input(f\"Quantify {product} product: \"))\n", + "\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "776c4213-6ffc-4d5b-9e1b-2cd1e8115cad", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Add selected product: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product book added to cart\n" + ] + } + ], + "source": [ + "#2a\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_order = []\n", + "\n", + "selected = input(\"Add selected product: \").lower()\n", + "\n", + "\n", + "if selected in products:\n", + " customer_order.append(selected)\n", + " print(f\"Product {selected} added to cart\")\n", + "else:\n", + " print(f\"Product not found\")\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "426cef37-1e3f-4678-b270-8957f0537faa", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Add selected product: t-shirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product t-shirt added to cart\n" + ] + } + ], + "source": [ + "#2b\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_order = set()\n", + "\n", + "selected = input(\"Add selected product: \").lower()\n", + "\n", + "\n", + "if selected in products:\n", + " customer_order.add(selected)\n", + " print(f\"Product {selected} added to cart\")\n", + "else:\n", + " print(f\"Product not found\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "ca020f45-be18-4b0c-8f1c-e2f7afeef249", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Add selected product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product mug added to cart\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Want other product?: yes\n" + ] + } + ], + "source": [ + "#2c\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_order = set()\n", + "\n", + "selected = input(\"Add selected product: \").lower()\n", + "\n", + "\n", + "if selected in products:\n", + " customer_order.add(selected)\n", + " print(f\"Product {selected} added to cart\")\n", + " other_product = input(\"Want other product?: \").lower\n", + "else:\n", + " print(f\"Product not found\")" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "5abb2ca1-4b63-45e4-82fe-8262bf76a249", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Add selected product: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product book added to cart\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Want other product? (yes/no): yes\n", + "Add selected product: t-shirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product t-shirt added to cart\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Want other product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thank you for your purchase\n", + "{'t-shirt', 'book'}\n" + ] + } + ], + "source": [ + "# 2d\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_order = set()\n", + "\n", + "other_product = \"yes\"\n", + "\n", + "while other_product == \"yes\":\n", + " selected = input(\"Add selected product: \").lower()\n", + "\n", + " if selected in products:\n", + " customer_order.add(selected)\n", + " print(f\"Product {selected} added to cart\")\n", + " else:\n", + " print(\"Product not found\")\n", + "\n", + " other_product = input(\"Want other product? (yes/no): \").lower()\n", + "\n", + "print(\"Thank you for your purchase\")\n", + "print(customer_order)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "d38dedf8-a7bb-417f-b3f6-fbb056a3f7ab", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 9, 'mug': 50, 'hat': 20, 'book': 49, 'keychain': 20}\n" + ] + } + ], + "source": [ + "for product in customer_order:\n", + " inventory[product] -=1\n", + "print(inventory)" + ] } ], "metadata": { @@ -55,7 +303,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,