From 884723e15af10ccef36214d78355099ae2613e95 Mon Sep 17 00:00:00 2001 From: Bibian <“bibian.glez@gmail.com”> Date: Thu, 7 May 2026 09:15:49 +0200 Subject: [PATCH] commiting lab --- lab-python-flow-control.ipynb | 423 +++++++++++++++++++++++++++++++++- 1 file changed, 420 insertions(+), 3 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..807f6a6 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,13 +37,430 @@ "\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": "ab33d10a-9e99-4fc3-849c-5599f7d2f10d", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5b4ceb60-81d8-42b0-a8e2-355df6bc47dc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# 1. Define a list called products that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "print(products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7b633083-7e25-44e3-b598-cee1b021fac8", + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Create an empty dictionary called inventory.\n", + "\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9c37f134-d0ad-4028-bb06-351796a02825", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 4\n", + "Enter the quantity for mug: 5\n", + "Enter the quantity for hat: 3\n", + "Enter the quantity for book: 4\n", + "Enter the quantity for keychain: 3\n" + ] + } + ], + "source": [ + "# 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.\n", + "\n", + "for item in products :\n", + " value = int(input(f\"Enter the quantity for {item}: \"))\n", + " inventory[item] = value" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6299f481-911e-41eb-9056-cb9b79dd3fcb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(value)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "fff383ae-3dbb-4db4-9400-9dbe1fe8b849", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 3, 'book': 4, 'keychain': 3}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "6537b560-ad8a-46dc-ad30-e45563e96f7c", + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Create an empty set called customer_orders\n", + "customer_orders = set()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "22d40038-5dd1-4733-bdd1-39a8148a4831", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product for the order: mug\n", + "Do you want to add another item? (yes/no): yes\n", + "Enter product for the order: hat\n", + "Do you want to add another item? (yes/no): yes\n", + "Enter product for the order: dog\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, 'dog' is not in our product list. Please try again.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product for the order: mug\n", + "Do you want to add another item? (yes/no): \n", + "Enter product for the order: yes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, 'yes' is not in our product list. Please try again.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product for the order: t-shirt\n", + "Do you want to add another item? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The customer orders are {'book', 't-shirt', 'mug', 'hat'}\n" + ] + } + ], + "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.\n", + "# b & c. Add the product name to the \"customer_orders\" set. Ask the user if they want to add another product (yes/no).\n", + "while True:\n", + " order = input(f\"Enter product for the order: \")\n", + " \n", + " if order in products:\n", + " customer_orders.add(order)\n", + " add_order = input(f\"Do you want to add another item? (yes/no):\")\n", + " \n", + " if \"yes\" in add_order:\n", + " continue\n", + " elif \"no\" in add_order:\n", + " print(f\"The customer orders are {customer_orders}\")\n", + " break\n", + " else:\n", + " print(f\"Sorry, '{order}' is not in our product list. Please try again.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d0a1c62c-d1a3-404f-baba-06c5583421cc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# 6. Print the products in the customer_orders set.\n", + "\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "16f180c8-b573-48fd-90c1-18e18968ae2d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "n_custom_o = len(customer_orders)\n", + "print(n_custom_o)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "a58d4d17-d18f-473b-ab6b-f2787d0d2a98", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "# 7. Calculate the following order statistics:\n", + "\n", + "# Total Products Ordered: The total number of products in the `customer_orders` set.\n", + "\n", + "total_products = len(customer_orders)\n", + "print(total_products)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "9112ca7f-5916-412f-90c0-40881de29f82", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "16\n" + ] + } + ], + "source": [ + "total_products = sum(int(value) for value in inventory.values() if str(value).isdigit())\n", + "print(total_products)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "27b61e57-8776-40f7-beae-83ed53fb455b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "18.75\n" + ] + } + ], + "source": [ + "# 7. Percentage of Products Ordered: The percentage of products ordered compared to the total available products\n", + "\n", + "# > there was not line where the number for each item ordered item was introduced, so I'll assume it was one of each \n", + "\n", + "# products ordered = 3\n", + "\n", + "percentage = (100*n_custom_o/total_products)\n", + "print(percentage)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "bab28bfb-1ad4-4b27-8b91-a4eac7049b3d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the percentage of purchased items is 18.75\n" + ] + } + ], + "source": [ + "print(f\"the percentage of purchased items is {percentage}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "033785d7-aafd-4acf-913f-fcc5388e6b87", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 16)\n" + ] + } + ], + "source": [ + "# Store these statistics in a tuple called `order_status`.\n", + "\n", + "order_status = (n_custom_o, total_products)\n", + "print(order_status)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "682becda-54d5-46fa-a44f-b40424791b07", + "metadata": {}, + "outputs": [], + "source": [ + "# 8. Print the order statistics using the following format:\n", + "# Total Products Ordered: \n", + "# Percentage of Products Ordered: % " + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "3b5769c9-f121-4744-bbc5-eee7f8541775", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "the percentage of purchased items is 18.75\n" + ] + } + ], + "source": [ + "print(f\"Total Products Ordered: {n_custom_o}\")\n", + "print(f\"the percentage of purchased items is {percentage}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "bf537426-671e-41ea-b99c-749ff89499bc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 't-shirt', 'mug', 'hat'}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "bc313758-6d2d-4d5f-99e7-209877110463", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 3, 'mug': 3, 'hat': 1, 'book': 2, 'keychain': 3}\n" + ] + } + ], + "source": [ + "# 3 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\").\n", + "for product in customer_orders:\n", + " inventory[product] -=1\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad1d09c7-877d-49f8-b6b5-b838743a17d3", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -55,7 +472,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,