From 288de09c747bf35d9effa85fbd1f96cc17998594 Mon Sep 17 00:00:00 2001 From: PabloFerreiraP Date: Wed, 16 Sep 2026 15:34:40 +0200 Subject: [PATCH 1/2] Complete Python flow control lab --- lab-python-flow-control.ipynb | 324 +++++++++++++++++++++++++++++++++- 1 file changed, 320 insertions(+), 4 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..92bee76 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -2,7 +2,6 @@ "cells": [ { "cell_type": "markdown", - "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", "metadata": { "tags": [] }, @@ -12,7 +11,6 @@ }, { "cell_type": "markdown", - "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", "metadata": {}, "source": [ "## Exercise: Managing Customer Orders Optimized\n", @@ -37,6 +35,324 @@ "\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": 98, + "metadata": {}, + "outputs": [], + "source": [ + "#1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "#2. Create an empty dictionary called inventory." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "#3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the products list as keys in the inventory dictionary and assign the respective quantities as values." + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter t-shirt quantity: 1\n", + "Enter mug quantity: 1\n", + "Enter hat quantity: 1\n", + "Enter book quantity: 1\n", + "Enter keychain quantity: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory: {'t-shirt': 1, 'mug': 1, 'hat': 1, 'book': 1, 'keychain': 1}\n" + ] + } + ], + "source": [ + "for item in products:\n", + " quantity = int(input(f\"Enter {item} quantity: \"))\n", + " inventory[item] = quantity\n", + "\n", + "print(\"Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "#4. Create an empty set calledcustomer_orders." + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "#5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "What would you like to order: t-shirt, mug, hat, book or keychain? mug\n", + "Do you want to add another product? (yes/no) yes\n", + "What would you like to order: t-shirt, mug, hat, book or keychain? book\n", + "Do you want to add another product? (yes/no) no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "\n", + "add_product = \"yes\"\n", + "\n", + "while add_product == \"yes\":\n", + " item = input(\"What would you like to order: t-shirt, mug, hat, book or keychain? \").lower()\n", + "\n", + " if item in products:\n", + " customer_orders.add(item)\n", + " else:\n", + " print(\"Invalid product or wrongly written. Please choose one from the list.\")\n", + "\n", + " add_product = input(\"Do you want to add another product? (yes/no) \").lower()\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "#6. Print the products in the customer_orders set." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer order: {'mug', 'book'}\n" + ] + } + ], + "source": [ + "print(\"Customer order: \" + str(customer_orders))" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "#7. Calculate the following order statistics: Total Products Ordered: The total number of products in the customer_orders set. Percentage of Products Ordered: The percentage of products ordered compared to the total available products. Store these statistics in a tuple called order_status." + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "40.0\n" + ] + } + ], + "source": [ + "total_products_ordered = len(customer_orders)\n", + "percentage_products_ordered = (total_products_ordered / sum(inventory.values())) * 100\n", + "\n", + "print(total_products_ordered)\n", + "print(percentage_products_ordered)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 40.0)" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "order_status = (\n", + " total_products_ordered,\n", + " percentage_products_ordered,)\n", + "\n", + "order_status" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "#8. Print the order statistics using the following format:" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.0\n" + ] + } + ], + "source": [ + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total_products_ordered)\n", + "print(\"Percentage of Products Ordered:\", percentage_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [], + "source": [ + "#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory: {'t-shirt': 1, 'mug': 0, 'hat': 1, 'book': 0, 'keychain': 1}\n" + ] + } + ], + "source": [ + "customer_orders\n", + "\n", + "for item in customer_orders:\n", + " inventory[item] -= 1\n", + "\n", + "print(\"Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [], + "source": [ + "#10. Print the updated inventory, displaying the quantity of each product on separate lines." + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T-Shirt: 1\n", + "Mug: 0\n", + "Hat: 1\n", + "Book: 0\n", + "Keychain: 1\n" + ] + } + ], + "source": [ + "for item, quantity in inventory.items():\n", + " print(item.title() + \":\", quantity)" + ] } ], "metadata": { @@ -55,9 +371,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4, - "nbformat_minor": 5 + "nbformat_minor": 4 } From 30562c135e1a837133499ff8992e1b0e76622c34 Mon Sep 17 00:00:00 2001 From: PabloFerreiraP Date: Wed, 16 Sep 2026 15:49:29 +0200 Subject: [PATCH 2/2] Update flow control lab --- lab-python-flow-control.ipynb | 293 +++++----------------------------- 1 file changed, 44 insertions(+), 249 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 92bee76..6055249 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -38,52 +38,7 @@ }, { "cell_type": "code", - "execution_count": 98, - "metadata": {}, - "outputs": [], - "source": [ - "#1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "#2. Create an empty dictionary called inventory." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "inventory = {}" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "#3. Ask the user to input the quantity of each product available in the inventory. Use the product names from the products list as keys in the inventory dictionary and assign the respective quantities as values." - ] - }, - { - "cell_type": "code", - "execution_count": 52, + "execution_count": 70, "metadata": {}, "outputs": [ { @@ -101,57 +56,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "Inventory: {'t-shirt': 1, 'mug': 1, 'hat': 1, 'book': 1, 'keychain': 1}\n" + "Inventory: {'t-shirt': 1, 'mug': 1, 'hat': 1, 'book': 1, 'keychain': 1}\n", + "\n" ] - } - ], - "source": [ - "for item in products:\n", - " quantity = int(input(f\"Enter {item} quantity: \"))\n", - " inventory[item] = quantity\n", - "\n", - "print(\"Inventory:\", inventory)" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": {}, - "outputs": [], - "source": [ - "#4. Create an empty set calledcustomer_orders." - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": {}, - "outputs": [], - "source": [ - "customer_orders = set()" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": {}, - "outputs": [], - "source": [ - "#5. Ask the user to input the name of three products that a customer wants to order (from those in the products list, meaning three products out of \"t-shirt\", \"mug\", \"hat\", \"book\" or \"keychain\". Add each product name to the customer_orders set." - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": {}, - "outputs": [ + }, { "name": "stdin", "output_type": "stream", "text": [ "What would you like to order: t-shirt, mug, hat, book or keychain? mug\n", "Do you want to add another product? (yes/no) yes\n", - "What would you like to order: t-shirt, mug, hat, book or keychain? book\n", + "What would you like to order: t-shirt, mug, hat, book or keychain? hat\n", "Do you want to add another product? (yes/no) no\n" ] }, @@ -159,11 +74,35 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'mug', 'book'}\n" + "Customer order: {'hat', 'mug'}\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.0\n", + "\n", + "Inventory: {'t-shirt': 1, 'mug': 0, 'hat': 0, 'book': 1, 'keychain': 1}\n", + "\n", + "T-Shirt: 1\n", + "Mug: 0\n", + "Hat: 0\n", + "Book: 1\n", + "Keychain: 1\n" ] } ], "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "\n", + "for item in products:\n", + " quantity = int(input(f\"Enter {item} quantity: \"))\n", + " inventory[item] = quantity\n", + "\n", + "print(\"Inventory:\", inventory)\n", + "\n", + "print(\"\")\n", + "\n", "customer_orders = set()\n", "\n", "add_product = \"yes\"\n", @@ -178,178 +117,34 @@ "\n", " add_product = input(\"Do you want to add another product? (yes/no) \").lower()\n", "\n", - "print(customer_orders)" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": {}, - "outputs": [], - "source": [ - "#6. Print the products in the customer_orders set." - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Customer order: {'mug', 'book'}\n" - ] - } - ], - "source": [ - "print(\"Customer order: \" + str(customer_orders))" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": {}, - "outputs": [], - "source": [ - "#7. Calculate the following order statistics: Total Products Ordered: The total number of products in the customer_orders set. Percentage of Products Ordered: The percentage of products ordered compared to the total available products. Store these statistics in a tuple called order_status." - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "40.0\n" - ] - } - ], - "source": [ + "print(\"Customer order: \" + str(customer_orders))\n", + "\n", + "print(\"\")\n", + "\n", "total_products_ordered = len(customer_orders)\n", "percentage_products_ordered = (total_products_ordered / sum(inventory.values())) * 100\n", "\n", - "print(total_products_ordered)\n", - "print(percentage_products_ordered)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 61, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(2, 40.0)" - ] - }, - "execution_count": 61, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ "order_status = (\n", " total_products_ordered,\n", " percentage_products_ordered,)\n", "\n", - "order_status" - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": {}, - "outputs": [], - "source": [ - "#8. Print the order statistics using the following format:" - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Order Statistics:\n", - "Total Products Ordered: 2\n", - "Percentage of Products Ordered: 40.0\n" - ] - } - ], - "source": [ + "order_status\n", + "\n", "print(\"Order Statistics:\")\n", "print(\"Total Products Ordered:\", total_products_ordered)\n", - "print(\"Percentage of Products Ordered:\", percentage_products_ordered)" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": {}, - "outputs": [], - "source": [ - "#9. Update the inventory by subtracting 1 from the quantity of each product. Modify the inventory dictionary accordingly." - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Inventory: {'t-shirt': 1, 'mug': 0, 'hat': 1, 'book': 0, 'keychain': 1}\n" - ] - } - ], - "source": [ + "print(\"Percentage of Products Ordered:\", percentage_products_ordered)\n", + "\n", + "print(\"\")\n", + "\n", "customer_orders\n", "\n", "for item in customer_orders:\n", " inventory[item] -= 1\n", "\n", - "print(\"Inventory:\", inventory)" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "metadata": {}, - "outputs": [], - "source": [ - "#10. Print the updated inventory, displaying the quantity of each product on separate lines." - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "T-Shirt: 1\n", - "Mug: 0\n", - "Hat: 1\n", - "Book: 0\n", - "Keychain: 1\n" - ] - } - ], - "source": [ + "print(\"Inventory:\", inventory)\n", + "\n", + "print(\"\")\n", + "\n", "for item, quantity in inventory.items():\n", " print(item.title() + \":\", quantity)" ]