From 7bbc71d783dc720faaea5a27e8dd3308ab392c90 Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Tue, 5 May 2026 09:52:14 +0200 Subject: [PATCH 1/5] Solved lab --- lab-python-data-structures.ipynb | 224 ++++++++++++++++++++++++++++++- 1 file changed, 223 insertions(+), 1 deletion(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..9d6500a4 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,6 +50,228 @@ "\n", "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [], + "source": [ + "inventory={}" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "how many t-shirts ? 5\n", + "how many mugs ? 3\n", + "how many hats ? 12\n", + "how many books ? 23\n", + "how many keychains ? 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 3, 'hat': 12, 'book': 23, 'keychain': 4}\n" + ] + } + ], + "source": [ + "for i in range(len(products)):\n", + " inventory[products[i]]=int(input('how many ' + products[i] + 's?'))\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders=[]" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "['mug']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product from ['t-shirt', 'hat', 'book', 'keychain'] book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "['mug', 'book']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product from ['t-shirt', 'hat', 'keychain'] hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "['mug', 'book', 'hat']\n" + ] + } + ], + "source": [ + "options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders=[]\n", + "for i in range(3):\n", + " choice= input('Choose a product from ' + str(options))\n", + " if choice in options:\n", + " customer_orders.append(choice)\n", + " options.remove(choice)\n", + " print(customer_orders)\n", + " else:\n", + " choice= input('Choose only a product from the list ' + str(options))\n", + " customer_orders.append(choice)\n", + " options.remove(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['mug', 'book', 'hat']\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "#print('Order Statistics')\n", + "total_number_products=len(customer_orders)\n", + "total=0\n", + "for i in products:\n", + " total=total+inventory[i]\n", + "percentage_ordered=round(3/total*100,1)\n", + "#print(f\"Total Products Ordered: {total_number_products}\")\n", + "#print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", + "order_status=('Total Products Ordered:',total_number_products,'Percentage of Products Ordered:',percentage_ordered)\n", + "#print(order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 7.3%\n" + ] + } + ], + "source": [ + "print('Order Statistics')\n", + "#total_number_products=len(customer_orders)\n", + "#total=0\n", + "#for i in products:\n", + "# total=total+inventory[i]\n", + "#percentage_ordered=round(3/total*100,1)\n", + "print(f\"Total Products Ordered: {total_number_products}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", + "#order_status=('Total Products Ordered:',total_number_products,'Percentage of Products Ordered:',percentage_ordered)\n", + "#print(order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 2, 'hat': 11, 'book': 22, 'keychain': 4}\n", + "{'t-shirt': 5, 'mug': 1, 'hat': 10, 'book': 21, 'keychain': 4}\n" + ] + } + ], + "source": [ + "for i in customer_orders:\n", + " inventory[i]-=1" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 1, 'hat': 10, 'book': 21, 'keychain': 4}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] } ], "metadata": { @@ -68,7 +290,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From de707af7bdba218770462b9bf9c326a9cfe84331 Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Tue, 5 May 2026 09:54:40 +0200 Subject: [PATCH 2/5] Solved lab --- ...ab-python-data-structures-checkpoint.ipynb | 298 ++++++++++++++++++ 1 file changed, 298 insertions(+) create mode 100644 .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb new file mode 100644 index 00000000..9d6500a4 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb @@ -0,0 +1,298 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Data Structures " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders\n", + "\n", + "As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", + "\n", + "2. Create an empty dictionary called `inventory`.\n", + "\n", + "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.\n", + "\n", + "4. Create an empty set called `customer_orders`.\n", + "\n", + "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", + "\n", + "6. Print the products in the `customer_orders` set.\n", + "\n", + "7. Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + " \n", + " Store these statistics in a tuple called `order_status`.\n", + "\n", + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " ```\n", + "\n", + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "\n", + "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + "\n", + "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [], + "source": [ + "inventory={}" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "how many t-shirts ? 5\n", + "how many mugs ? 3\n", + "how many hats ? 12\n", + "how many books ? 23\n", + "how many keychains ? 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 3, 'hat': 12, 'book': 23, 'keychain': 4}\n" + ] + } + ], + "source": [ + "for i in range(len(products)):\n", + " inventory[products[i]]=int(input('how many ' + products[i] + 's?'))\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders=[]" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "['mug']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product from ['t-shirt', 'hat', 'book', 'keychain'] book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "['mug', 'book']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product from ['t-shirt', 'hat', 'keychain'] hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "['mug', 'book', 'hat']\n" + ] + } + ], + "source": [ + "options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders=[]\n", + "for i in range(3):\n", + " choice= input('Choose a product from ' + str(options))\n", + " if choice in options:\n", + " customer_orders.append(choice)\n", + " options.remove(choice)\n", + " print(customer_orders)\n", + " else:\n", + " choice= input('Choose only a product from the list ' + str(options))\n", + " customer_orders.append(choice)\n", + " options.remove(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['mug', 'book', 'hat']\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "#print('Order Statistics')\n", + "total_number_products=len(customer_orders)\n", + "total=0\n", + "for i in products:\n", + " total=total+inventory[i]\n", + "percentage_ordered=round(3/total*100,1)\n", + "#print(f\"Total Products Ordered: {total_number_products}\")\n", + "#print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", + "order_status=('Total Products Ordered:',total_number_products,'Percentage of Products Ordered:',percentage_ordered)\n", + "#print(order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 7.3%\n" + ] + } + ], + "source": [ + "print('Order Statistics')\n", + "#total_number_products=len(customer_orders)\n", + "#total=0\n", + "#for i in products:\n", + "# total=total+inventory[i]\n", + "#percentage_ordered=round(3/total*100,1)\n", + "print(f\"Total Products Ordered: {total_number_products}\")\n", + "print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", + "#order_status=('Total Products Ordered:',total_number_products,'Percentage of Products Ordered:',percentage_ordered)\n", + "#print(order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 2, 'hat': 11, 'book': 22, 'keychain': 4}\n", + "{'t-shirt': 5, 'mug': 1, 'hat': 10, 'book': 21, 'keychain': 4}\n" + ] + } + ], + "source": [ + "for i in customer_orders:\n", + " inventory[i]-=1" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 1, 'hat': 10, 'book': 21, 'keychain': 4}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} From 85750f1d65a2e2c4193a0111081f60774e1ac665 Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Tue, 5 May 2026 16:42:39 +0200 Subject: [PATCH 3/5] Solved lab --- lab-python-data-structures.ipynb | 53 +++++++++++++++----------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 9d6500a4..a7b30966 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -110,62 +110,59 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ - "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n" + "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] hat\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "0\n", - "['mug']\n" + "{'hat'}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Choose a product from ['t-shirt', 'hat', 'book', 'keychain'] book\n" + "Choose a product from ['t-shirt', 'mug', 'book', 'keychain'] mug\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "1\n", - "['mug', 'book']\n" + "{'hat', 'mug'}\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Choose a product from ['t-shirt', 'hat', 'keychain'] hat\n" + "Choose a product from ['t-shirt', 'book', 'keychain'] book\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "2\n", - "['mug', 'book', 'hat']\n" + "{'hat', 'mug', 'book'}\n" ] } ], "source": [ "options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", - "customer_orders=[]\n", + "customer_orders=set()\n", "for i in range(3):\n", " choice= input('Choose a product from ' + str(options))\n", " if choice in options:\n", - " customer_orders.append(choice)\n", + " customer_orders.add(choice)\n", " options.remove(choice)\n", " print(customer_orders)\n", " else:\n", @@ -176,14 +173,14 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "['mug', 'book', 'hat']\n" + "{'hat', 'mug', 'book'}\n" ] } ], @@ -193,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 89, "metadata": {}, "outputs": [], "source": [ @@ -211,7 +208,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 90, "metadata": {}, "outputs": [ { @@ -239,18 +236,9 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 91, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'t-shirt': 5, 'mug': 2, 'hat': 11, 'book': 22, 'keychain': 4}\n", - "{'t-shirt': 5, 'mug': 1, 'hat': 10, 'book': 21, 'keychain': 4}\n" - ] - } - ], + "outputs": [], "source": [ "for i in customer_orders:\n", " inventory[i]-=1" @@ -258,20 +246,27 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 5, 'mug': 1, 'hat': 10, 'book': 21, 'keychain': 4}\n" + "{'t-shirt': 5, 'mug': 0, 'hat': 9, 'book': 20, 'keychain': 4}\n" ] } ], "source": [ "print(inventory)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { From d2d566e893c81785dd1e180865a2f36a91832612 Mon Sep 17 00:00:00 2001 From: claireley Date: Tue, 5 May 2026 16:48:53 +0200 Subject: [PATCH 4/5] Delete .ipynb_checkpoints directory --- ...ab-python-data-structures-checkpoint.ipynb | 298 ------------------ 1 file changed, 298 deletions(-) delete mode 100644 .ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb deleted file mode 100644 index 9d6500a4..00000000 --- a/.ipynb_checkpoints/lab-python-data-structures-checkpoint.ipynb +++ /dev/null @@ -1,298 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "tags": [] - }, - "source": [ - "# Lab | Data Structures " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exercise: Managing Customer Orders\n", - "\n", - "As part of a business venture, you are starting an online store that sells various products. To ensure smooth operations, you need to develop a program that manages customer orders and inventory.\n", - "\n", - "Follow the steps below to complete the exercise:\n", - "\n", - "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\".\n", - "\n", - "2. Create an empty dictionary called `inventory`.\n", - "\n", - "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.\n", - "\n", - "4. Create an empty set called `customer_orders`.\n", - "\n", - "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", - "\n", - "6. Print the products in the `customer_orders` set.\n", - "\n", - "7. Calculate the following order statistics:\n", - " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", - " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", - " \n", - " Store these statistics in a tuple called `order_status`.\n", - "\n", - "8. Print the order statistics using the following format:\n", - " ```\n", - " Order Statistics:\n", - " Total Products Ordered: \n", - " Percentage of Products Ordered: % \n", - " ```\n", - "\n", - "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", - "\n", - "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", - "\n", - "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": {}, - "outputs": [], - "source": [ - "products=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": {}, - "outputs": [], - "source": [ - "inventory={}" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": {}, - "outputs": [ - { - "name": "stdin", - "output_type": "stream", - "text": [ - "how many t-shirts ? 5\n", - "how many mugs ? 3\n", - "how many hats ? 12\n", - "how many books ? 23\n", - "how many keychains ? 4\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'t-shirt': 5, 'mug': 3, 'hat': 12, 'book': 23, 'keychain': 4}\n" - ] - } - ], - "source": [ - "for i in range(len(products)):\n", - " inventory[products[i]]=int(input('how many ' + products[i] + 's?'))\n", - "print(inventory)" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": {}, - "outputs": [], - "source": [ - "customer_orders=[]" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": {}, - "outputs": [ - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] mug\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "['mug']\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Choose a product from ['t-shirt', 'hat', 'book', 'keychain'] book\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "['mug', 'book']\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "Choose a product from ['t-shirt', 'hat', 'keychain'] hat\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "['mug', 'book', 'hat']\n" - ] - } - ], - "source": [ - "options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", - "customer_orders=[]\n", - "for i in range(3):\n", - " choice= input('Choose a product from ' + str(options))\n", - " if choice in options:\n", - " customer_orders.append(choice)\n", - " options.remove(choice)\n", - " print(customer_orders)\n", - " else:\n", - " choice= input('Choose only a product from the list ' + str(options))\n", - " customer_orders.append(choice)\n", - " options.remove(choice)" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['mug', 'book', 'hat']\n" - ] - } - ], - "source": [ - "print(customer_orders)" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": {}, - "outputs": [], - "source": [ - "#print('Order Statistics')\n", - "total_number_products=len(customer_orders)\n", - "total=0\n", - "for i in products:\n", - " total=total+inventory[i]\n", - "percentage_ordered=round(3/total*100,1)\n", - "#print(f\"Total Products Ordered: {total_number_products}\")\n", - "#print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", - "order_status=('Total Products Ordered:',total_number_products,'Percentage of Products Ordered:',percentage_ordered)\n", - "#print(order_status)" - ] - }, - { - "cell_type": "code", - "execution_count": 82, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Order Statistics\n", - "Total Products Ordered: 3\n", - "Percentage of Products Ordered: 7.3%\n" - ] - } - ], - "source": [ - "print('Order Statistics')\n", - "#total_number_products=len(customer_orders)\n", - "#total=0\n", - "#for i in products:\n", - "# total=total+inventory[i]\n", - "#percentage_ordered=round(3/total*100,1)\n", - "print(f\"Total Products Ordered: {total_number_products}\")\n", - "print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", - "#order_status=('Total Products Ordered:',total_number_products,'Percentage of Products Ordered:',percentage_ordered)\n", - "#print(order_status)" - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'t-shirt': 5, 'mug': 2, 'hat': 11, 'book': 22, 'keychain': 4}\n", - "{'t-shirt': 5, 'mug': 1, 'hat': 10, 'book': 21, 'keychain': 4}\n" - ] - } - ], - "source": [ - "for i in customer_orders:\n", - " inventory[i]-=1" - ] - }, - { - "cell_type": "code", - "execution_count": 83, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'t-shirt': 5, 'mug': 1, 'hat': 10, 'book': 21, 'keychain': 4}\n" - ] - } - ], - "source": [ - "print(inventory)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.13.9" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} From 2404751019469e58c43f1cbd0efeabf6e62c4b7b Mon Sep 17 00:00:00 2001 From: Claire Leyden Date: Wed, 6 May 2026 10:39:25 +0200 Subject: [PATCH 5/5] Solved lab --- lab-python-data-structures.ipynb | 93 +++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 26 deletions(-) diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index a7b30966..14ed002b 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -105,7 +105,7 @@ "metadata": {}, "outputs": [], "source": [ - "customer_orders=[]" + "customer_orders=set()" ] }, { @@ -156,19 +156,61 @@ ] } ], + "source": [ + "# options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "# customer_orders=set()\n", + "# for i in range(3):\n", + "# choice= input('Choose a product from ' + str(options))\n", + "# if choice in options:\n", + "# customer_orders.add(choice)\n", + "# options.remove(choice)\n", + "# print(customer_orders)\n", + "# else:\n", + "# choice= input('Choose only a product from the list ' + str(options))\n", + "# customer_orders.append(choice)\n", + "# options.remove(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] shoes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not a valid item\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product from ['t-shirt', 'mug', 'hat', 'book', 'keychain'] book\n", + "Choose a product from ['t-shirt', 'mug', 'hat', 'keychain'] hat\n", + "Choose a product from ['t-shirt', 'mug', 'keychain'] mug\n" + ] + } + ], "source": [ "options=[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "customer_orders=set()\n", - "for i in range(3):\n", - " choice= input('Choose a product from ' + str(options))\n", + "while len(customer_orders)!=3:\n", + " choice= input('Choose one product from ' + str(options))\n", " if choice in options:\n", " customer_orders.add(choice)\n", " options.remove(choice)\n", - " print(customer_orders)\n", + " #print(customer_orders)\n", " else:\n", - " choice= input('Choose only a product from the list ' + str(options))\n", - " customer_orders.append(choice)\n", - " options.remove(choice)" + " print('Not a valid item, try again')" ] }, { @@ -190,15 +232,15 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "#print('Order Statistics')\n", "total_number_products=len(customer_orders)\n", - "total=0\n", - "for i in products:\n", - " total=total+inventory[i]\n", + "total=sum(inventory.values())\n", + "#for i in products:\n", + " # total=total+inventory[i]\n", "percentage_ordered=round(3/total*100,1)\n", "#print(f\"Total Products Ordered: {total_number_products}\")\n", "#print(f\"Percentage of Products Ordered: {percentage_ordered}%\" )\n", @@ -208,7 +250,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 105, "metadata": {}, "outputs": [ { @@ -217,7 +259,7 @@ "text": [ "Order Statistics\n", "Total Products Ordered: 3\n", - "Percentage of Products Ordered: 7.3%\n" + "Percentage of Products Ordered: 9.1%\n" ] } ], @@ -236,37 +278,36 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 113, "metadata": {}, "outputs": [], "source": [ - "for i in customer_orders:\n", - " inventory[i]-=1" + "for i in products: #customer_orders:\n", + " if inventory[i]!=0:\n", + " inventory[i]-=1" ] }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 5, 'mug': 0, 'hat': 9, 'book': 20, 'keychain': 4}\n" + "There are 4 t-shirts\n", + "There are -1 mugs\n", + "There are 8 hats\n", + "There are 19 books\n", + "There are 3 keychains\n" ] } ], "source": [ - "print(inventory)" + "for key,value in inventory.items():\n", + " print(f\"There are {value} {key}s\")" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {