From 8dad705b52e785f4a928ba3b78bdde3a3fa4e01e Mon Sep 17 00:00:00 2001 From: GarriguesElena Date: Fri, 19 Jun 2026 16:08:25 +0200 Subject: [PATCH] functions lab solved --- .../lab-python-functions-checkpoint.ipynb | 514 ++++++++++++++++++ lab-python-functions.ipynb | 451 ++++++++++++++- 2 files changed, 962 insertions(+), 3 deletions(-) create mode 100644 .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..a61eb3e --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,514 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Functions" + ] + }, + { + "cell_type": "markdown", + "id": "0c581062-8967-4d93-b06e-62833222f930", + "metadata": { + "tags": [] + }, + "source": [ + "## Exercise: Managing Customer Orders with Functions\n", + "\n", + "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", + "\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", + "\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", + "\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", + "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", + "\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", + "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", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7158e14c-c9da-45cf-854c-8e2dcc7c8f7b", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "a988f0a9-3e7a-446b-97e8-ae683b2c2044", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mug\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "book\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 6, 'hat': 8, 'book': 4, 'keychain': 8}\n" + ] + } + ], + "source": [ + "inventory = {}\n", + "\n", + "def initialize_inventory(products):\n", + " for product in products:\n", + " print(product)\n", + " new_product = int(input(\"Enter the number of products: \"))\n", + " inventory[product] = new_product\n", + "\n", + "initialize_inventory(products)\n", + "print(inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9c831d00-bebc-4811-85a9-99220e03a879", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: mug\n", + "Would you like to add a product? yes/no yes\n", + "Enter the product name: hat\n", + "Would you like to add a product? yes/no yes\n", + "Enter the product name: t-shirt\n", + "Would you like to add a product? yes/no no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt', 'hat'}\n" + ] + } + ], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " product = input(\"Enter the product name: \")\n", + " customer_orders.add(product)\n", + " new_product = input(\"Would you like to add a product? yes/no\")\n", + " while new_product == \"yes\":\n", + " product = input(\"Enter the product name: \")\n", + " customer_orders.add(product)\n", + " new_product = input(\"Would you like to add a product? yes/no\")\n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "95940fb4-49dd-44ab-9719-17fcb13e7934", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 6, 'hat': 8, 'book': 4, 'keychain': 8}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f2b802ec-c2a5-476a-beee-fc70a7bf81b6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt', 'hat'}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "b1cc36f3-6ec3-4164-8d21-b2c4c066c33d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 2, 'mug': 4, 'hat': 6, 'book': 3, 'keychain': 7}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] = inventory[product] -1\n", + " return inventory\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "33a81096-d1c7-40f1-8075-e9056fd3a4d2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = ((len(customer_orders)) / (len(products))) * 100\n", + " order_statistics = total_products_ordered, percentage_ordered\n", + " return order_statistics\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "601d0eca-c5b5-483a-94ee-8628a7bd19d2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "61de90bb-1b04-4994-bb83-eed5018421ce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0 %\n" + ] + } + ], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_products_ordered = order_statistics[0]\n", + " percentage_ordered = order_statistics[1]\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total_products_ordered)\n", + " print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")\n", + "\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "732952d8-7eb5-4129-8207-030cb1e05a8c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 7, 'book': 4, 'keychain': 8}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(update_inventory)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "24f5c3b7-d5a7-4cd7-911d-e8c439301f6d", + "metadata": {}, + "outputs": [], + "source": [ + "def program(inventory, customer_orders):\n", + " print(\" Hello! Please input the quantity of each product:\")\n", + " initialize_inventory(products)\n", + " print()\n", + " print(\"Please enter the products the customer has ordered:\")\n", + " get_customer_orders()\n", + " print(f\"This is the customer orders list: {customer_orders}\")\n", + " update_inventory(customer_orders, inventory)\n", + " print(f\"This is your updated inventory {inventory}\")\n", + " print()\n", + " print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "32ee6a20-e15e-4d07-b20d-eb0460678aaf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Hello! Please input the quantity of each product:\n", + "t-shirt\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mug\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "book\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Please enter the products the customer has ordered:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: t-shirt\n", + "Would you like to add a product? yes/no yes\n", + "Enter the product name: book\n", + "Would you like to add a product? yes/no no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the customer orders list: {'mug', 't-shirt', 'hat'}\n", + "This is your updated inventory{'t-shirt': 0, 'mug': 3, 'hat': 5, 'book': 7, 'keychain': 4}\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0 %\n" + ] + } + ], + "source": [ + "program(inventory, customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "41a36b60-9ca6-4239-a816-0ac5fbf6af84", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..a61eb3e 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,458 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7158e14c-c9da-45cf-854c-8e2dcc7c8f7b", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "a988f0a9-3e7a-446b-97e8-ae683b2c2044", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mug\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "book\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 6, 'hat': 8, 'book': 4, 'keychain': 8}\n" + ] + } + ], + "source": [ + "inventory = {}\n", + "\n", + "def initialize_inventory(products):\n", + " for product in products:\n", + " print(product)\n", + " new_product = int(input(\"Enter the number of products: \"))\n", + " inventory[product] = new_product\n", + "\n", + "initialize_inventory(products)\n", + "print(inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9c831d00-bebc-4811-85a9-99220e03a879", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: mug\n", + "Would you like to add a product? yes/no yes\n", + "Enter the product name: hat\n", + "Would you like to add a product? yes/no yes\n", + "Enter the product name: t-shirt\n", + "Would you like to add a product? yes/no no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt', 'hat'}\n" + ] + } + ], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " product = input(\"Enter the product name: \")\n", + " customer_orders.add(product)\n", + " new_product = input(\"Would you like to add a product? yes/no\")\n", + " while new_product == \"yes\":\n", + " product = input(\"Enter the product name: \")\n", + " customer_orders.add(product)\n", + " new_product = input(\"Would you like to add a product? yes/no\")\n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "95940fb4-49dd-44ab-9719-17fcb13e7934", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 6, 'hat': 8, 'book': 4, 'keychain': 8}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f2b802ec-c2a5-476a-beee-fc70a7bf81b6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt', 'hat'}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "b1cc36f3-6ec3-4164-8d21-b2c4c066c33d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 2, 'mug': 4, 'hat': 6, 'book': 3, 'keychain': 7}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] = inventory[product] -1\n", + " return inventory\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "33a81096-d1c7-40f1-8075-e9056fd3a4d2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = ((len(customer_orders)) / (len(products))) * 100\n", + " order_statistics = total_products_ordered, percentage_ordered\n", + " return order_statistics\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "601d0eca-c5b5-483a-94ee-8628a7bd19d2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "61de90bb-1b04-4994-bb83-eed5018421ce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0 %\n" + ] + } + ], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_products_ordered = order_statistics[0]\n", + " percentage_ordered = order_statistics[1]\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total_products_ordered)\n", + " print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")\n", + "\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "732952d8-7eb5-4129-8207-030cb1e05a8c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 7, 'book': 4, 'keychain': 8}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(update_inventory)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "24f5c3b7-d5a7-4cd7-911d-e8c439301f6d", + "metadata": {}, + "outputs": [], + "source": [ + "def program(inventory, customer_orders):\n", + " print(\" Hello! Please input the quantity of each product:\")\n", + " initialize_inventory(products)\n", + " print()\n", + " print(\"Please enter the products the customer has ordered:\")\n", + " get_customer_orders()\n", + " print(f\"This is the customer orders list: {customer_orders}\")\n", + " update_inventory(customer_orders, inventory)\n", + " print(f\"This is your updated inventory {inventory}\")\n", + " print()\n", + " print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "32ee6a20-e15e-4d07-b20d-eb0460678aaf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Hello! Please input the quantity of each product:\n", + "t-shirt\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mug\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hat\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "book\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "keychain\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of products: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Please enter the products the customer has ordered:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: t-shirt\n", + "Would you like to add a product? yes/no yes\n", + "Enter the product name: book\n", + "Would you like to add a product? yes/no no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the customer orders list: {'mug', 't-shirt', 'hat'}\n", + "This is your updated inventory{'t-shirt': 0, 'mug': 3, 'hat': 5, 'book': 7, 'keychain': 4}\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0 %\n" + ] + } + ], + "source": [ + "program(inventory, customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "41a36b60-9ca6-4239-a816-0ac5fbf6af84", + "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": { @@ -61,7 +506,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,