From 64aee04bdd590518a359ad2f799461d431b056d6 Mon Sep 17 00:00:00 2001 From: Eylin Date: Mon, 19 Jan 2026 15:21:30 +0100 Subject: [PATCH 1/3] Lab python functions Eylin --- lab-python-functions.ipynb | 269 +++++++++++++++++++++++++++++++++++-- 1 file changed, 255 insertions(+), 14 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..2abb2c4 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -20,34 +20,275 @@ "In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n", "\n", "Follow the steps below to complete the exercise:\n", + "7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", "\n", - "1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n", - "\n", - "2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n", + "Hints for functions:\n", "\n", - "3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n", + "- Consider the input parameters required for each function and their return values.\n", + "- Utilize function parameters and return values to transfer data between functions.\n", + "- Test your functions individually to ensure they work correctly.\n", "\n", - "4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "1b7f10d7", + "metadata": {}, + "source": [ + "1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba8a897e", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {'mug':0, 'hat':0, 'keychain':0, 'book':0, 't-shirt':0}\n", "\n", - "5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n", + "def initialize_inventory(products):\n", + " for item in inventory:\n", + " products = input (\"Enter a value\")\n", + " if products in inventory:\n", + " value = (int(input(\"Enter a value\")))\n", + " inventory[products]=value\n", + " \n", + " \n", + " return products\n", "\n", - "6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "id": "1df42982", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n" + ] + } + ], + "source": [ + "products = inventory\n", + "initialize_inventory (products)\n", + "print(products)\n", "\n", - "7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", "\n", - "Hints for functions:\n", "\n", - "- Consider the input parameters required for each function and their return values.\n", - "- Utilize function parameters and return values to transfer data between functions.\n", - "- Test your functions individually to ensure they work correctly.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "6610d470", + "metadata": {}, + "source": [ + "2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4300e4a8", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()\n", "\n", + "def get_customers_orders(): \n", + " \n", + " continue_buying = \"Yes\"\n", + " \n", + " while continue_buying == \"Yes\":\n", + " product = input(\"What do you want to buy?: \")\n", + " customer_orders.add(product)\n", + " continue_buying = input(\"Do you want to continue buying? (Yes/No): \")\n", + " \n", + " return customer_orders\n", + " \n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 162, + "id": "70b11c12", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt'}\n", + "Thanks for your purchase.\n" + ] + } + ], + "source": [ + "get_customers_orders()\n", + "print(customer_orders)\n", + "print(\"Thanks for your purchase.\")" + ] + }, + { + "cell_type": "markdown", + "id": "20638e0d", + "metadata": {}, + "source": [ + "3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders." + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "9f784305", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}\n" + ] + } + ], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", + "customer_orders = ('mug', 't-shirt')\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \n", + " for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item]-=1\n", + "\n", + " return customer_orders, inventory\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "print(inventory)\n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "b7a40557", + "metadata": {}, + "source": [ + "4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered (Number of different products)). The function should return these values." + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "5489e00c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 40.0)" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", + "customer_orders = ('mug', 't-shirt')\n", + "\n", + "def calculate_order_statistics(customer_orders, inventory):\n", + " total_products_ordered = len(customer_orders)\n", + " total_products_inventory = len(inventory)\n", + " percent_unique_products = (total_products_ordered/total_products_inventory)*100\n", + " \n", + " return total_products_ordered, percent_unique_products\n", + "\n", + "calculate_order_statistics(customer_orders, inventory)\n" + ] + }, + { + "cell_type": "markdown", + "id": "35a81169", + "metadata": {}, + "source": [ + "5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics." + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "61448a34", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " return order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "69891f1d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total_products_ordered = order_statistics\n", + "print_order_statistics(total_products_ordered)" + ] + }, + { + "cell_type": "markdown", + "id": "9dc024e1", + "metadata": {}, + "source": [ + "6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory." + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "87f8554a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " return inventory\n", + "print(inventory)\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +302,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From 318d0643c5450405ce4b8ad6ead6196ae210e471 Mon Sep 17 00:00:00 2001 From: Eylin Date: Thu, 5 Mar 2026 21:39:05 +0100 Subject: [PATCH 2/3] new changes --- lab-python-functions.ipynb | 145 +++++++++++++++++++++---------------- 1 file changed, 81 insertions(+), 64 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 2abb2c4..2501db6 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -41,29 +41,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "id": "ba8a897e", "metadata": {}, "outputs": [], "source": [ - "inventory = {'mug':0, 'hat':0, 'keychain':0, 'book':0, 't-shirt':0}\n", + "inventory_list = ['mug', 'hat', 'keychain', 'book', 't-shirt']\n", "\n", "def initialize_inventory(products):\n", - " for item in inventory:\n", - " products = input (\"Enter a value\")\n", - " if products in inventory:\n", - " value = (int(input(\"Enter a value\")))\n", - " inventory[products]=value\n", - " \n", - " \n", - " return products\n", - "\n", + " inventory = {}\n", + " for product in products: \n", + " qty = int(input(f\"Enter quantity for {product}: \")) \n", + " inventory[product] = qty\n", + " return inventory\n", "\n" ] }, { "cell_type": "code", - "execution_count": 138, + "execution_count": 63, "id": "1df42982", "metadata": {}, "outputs": [ @@ -76,13 +72,9 @@ } ], "source": [ - "products = inventory\n", - "initialize_inventory (products)\n", - "print(products)\n", - "\n", - "\n", - "\n", - "\n" + "# Correct function call\n", + "inventory = initialize_inventory(inventory_list)\n", + "print(inventory)" ] }, { @@ -95,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "4300e4a8", "metadata": {}, "outputs": [], @@ -118,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 162, + "execution_count": 40, "id": "70b11c12", "metadata": {}, "outputs": [ @@ -126,14 +118,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'mug', 't-shirt'}\n", "Thanks for your purchase.\n" ] } ], "source": [ "get_customers_orders()\n", - "print(customer_orders)\n", "print(\"Thanks for your purchase.\")" ] }, @@ -147,18 +137,10 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 43, "id": "9f784305", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}\n" - ] - } - ], + "outputs": [], "source": [ "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", "customer_orders = ('mug', 't-shirt')\n", @@ -169,14 +151,34 @@ " if item in inventory:\n", " inventory[item]-=1\n", "\n", - " return customer_orders, inventory\n", + " return inventory\n", + "\n", "\n", - "update_inventory(customer_orders, inventory)\n", - "print(inventory)\n", "\n", " " ] }, + { + "cell_type": "code", + "execution_count": 44, + "id": "60c923cb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)\n" + ] + }, { "cell_type": "markdown", "id": "b7a40557", @@ -187,9 +189,27 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 45, "id": "5489e00c", "metadata": {}, + "outputs": [], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", + "customer_orders = ('mug', 't-shirt')\n", + "\n", + "def calculate_order_statistics(customer_orders, inventory):\n", + " total_products_ordered = len(customer_orders)\n", + " total_products_inventory = len(inventory)\n", + " percent_unique_products = (total_products_ordered/total_products_inventory)*100\n", + " \n", + " return total_products_ordered, percent_unique_products\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e98c3fe7", + "metadata": {}, "outputs": [ { "data": { @@ -197,22 +217,12 @@ "(2, 40.0)" ] }, - "execution_count": 86, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", - "customer_orders = ('mug', 't-shirt')\n", - "\n", - "def calculate_order_statistics(customer_orders, inventory):\n", - " total_products_ordered = len(customer_orders)\n", - " total_products_inventory = len(inventory)\n", - " percent_unique_products = (total_products_ordered/total_products_inventory)*100\n", - " \n", - " return total_products_ordered, percent_unique_products\n", - "\n", "calculate_order_statistics(customer_orders, inventory)\n" ] }, @@ -226,35 +236,42 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 75, "id": "61448a34", "metadata": {}, "outputs": [], "source": [ + "def calculate_order_statistics(customer_orders, inventory):\n", + " total_products_ordered = len(set(customer_orders))\n", + " percentage_ordered = (total_products_ordered / len(inventory)) * 100\n", + " return total_products_ordered, percentage_ordered\n", + "\n", "def print_order_statistics(order_statistics):\n", - " return order_statistics" + " print(f\"Total unique products ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of unique products ordered: {order_statistics[1]:.2f}%\")\n" ] }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 78, "id": "69891f1d", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 99, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Total unique products ordered: 2\n", + "Percentage of unique products ordered: 40.00%\n" + ] } ], "source": [ - "total_products_ordered = order_statistics\n", - "print_order_statistics(total_products_ordered)" + "# Calculating statistics based on your data\n", + "order_stats = calculate_order_statistics(customer_orders, inventory)\n", + "\n", + "# Printing those statistics\n", + "print_order_statistics(order_stats)" ] }, { @@ -267,7 +284,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": null, "id": "87f8554a", "metadata": {}, "outputs": [ @@ -281,8 +298,8 @@ ], "source": [ "def print_updated_inventory(inventory):\n", - " return inventory\n", - "print(inventory)\n" + " for product, qty in inventory.items():\n", + " print (f\"{product}: {qty}\")\n" ] } ], From 4b8e7789be6bb207f0d9ac0486c3591b73efd014 Mon Sep 17 00:00:00 2001 From: Eylin Date: Thu, 5 Mar 2026 21:49:17 +0100 Subject: [PATCH 3/3] new changes --- lab-python-functions.ipynb | 211 ++++++++++++++++++------------------- 1 file changed, 101 insertions(+), 110 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 2501db6..98a30c3 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "id": "ba8a897e", "metadata": {}, "outputs": [], @@ -53,28 +53,7 @@ " for product in products: \n", " qty = int(input(f\"Enter quantity for {product}: \")) \n", " inventory[product] = qty\n", - " return inventory\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "id": "1df42982", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n" - ] - } - ], - "source": [ - "# Correct function call\n", - "inventory = initialize_inventory(inventory_list)\n", - "print(inventory)" + " return inventory" ] }, { @@ -87,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "id": "4300e4a8", "metadata": {}, "outputs": [], @@ -103,28 +82,7 @@ " customer_orders.add(product)\n", " continue_buying = input(\"Do you want to continue buying? (Yes/No): \")\n", " \n", - " return customer_orders\n", - " \n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "id": "70b11c12", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Thanks for your purchase.\n" - ] - } - ], - "source": [ - "get_customers_orders()\n", - "print(\"Thanks for your purchase.\")" + " return customer_orders" ] }, { @@ -137,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "id": "9f784305", "metadata": {}, "outputs": [], @@ -151,32 +109,7 @@ " if item in inventory:\n", " inventory[item]-=1\n", "\n", - " return inventory\n", - "\n", - "\n", - "\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "id": "60c923cb", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "update_inventory(customer_orders, inventory)\n" + " return inventory" ] }, { @@ -205,27 +138,6 @@ " return total_products_ordered, percent_unique_products\n" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "e98c3fe7", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(2, 40.0)" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "calculate_order_statistics(customer_orders, inventory)\n" - ] - }, { "cell_type": "markdown", "id": "35a81169", @@ -251,55 +163,134 @@ " print(f\"Percentage of unique products ordered: {order_statistics[1]:.2f}%\")\n" ] }, + { + "cell_type": "markdown", + "id": "9dc024e1", + "metadata": {}, + "source": [ + "6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory." + ] + }, { "cell_type": "code", - "execution_count": 78, - "id": "69891f1d", + "execution_count": null, + "id": "87f8554a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Total unique products ordered: 2\n", - "Percentage of unique products ordered: 40.00%\n" + "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}\n" ] } ], "source": [ - "# Calculating statistics based on your data\n", - "order_stats = calculate_order_statistics(customer_orders, inventory)\n", - "\n", - "# Printing those statistics\n", - "print_order_statistics(order_stats)" + "def print_updated_inventory(inventory):\n", + " for product, qty in inventory.items():\n", + " print (f\"{product}: {qty}\")\n" ] }, { "cell_type": "markdown", - "id": "9dc024e1", + "id": "33520633", "metadata": {}, "source": [ - "6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory." + "7. Calling Functions" ] }, { "cell_type": "code", "execution_count": null, - "id": "87f8554a", + "id": "606189c9", + "metadata": {}, + "outputs": [], + "source": [ + "# Correct function call\n", + "inventory = initialize_inventory(inventory_list)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d390065f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}\n" + "Thanks for your purchase.\n" ] } ], "source": [ - "def print_updated_inventory(inventory):\n", - " for product, qty in inventory.items():\n", - " print (f\"{product}: {qty}\")\n" + "get_customers_orders()\n", + "print(\"Thanks for your purchase.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1c25c92", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9fc59c64", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 40.0)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "calculate_order_statistics(customer_orders, inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eaf3f015", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total unique products ordered: 2\n", + "Percentage of unique products ordered: 40.00%\n" + ] + } + ], + "source": [ + "# Calculating statistics based on your data\n", + "order_stats = calculate_order_statistics(customer_orders, inventory)\n", + "\n", + "# Printing those statistics\n", + "print_order_statistics(order_stats)" ] } ],