From 9bbb57a0044781f1374c22074af1440ff6ce2fed Mon Sep 17 00:00:00 2001 From: MartaMX Date: Sun, 21 Jun 2026 11:17:14 +0200 Subject: [PATCH] Completed lab --- .../lab-python-functions-checkpoint.ipynb | 375 ++++++++++++++++++ lab-python-functions.ipynb | 308 +++++++++++++- 2 files changed, 682 insertions(+), 1 deletion(-) 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..09bbfcb --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,375 @@ +{ + "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": "markdown", + "id": "ce063cd0-d24a-482f-aebf-e168f0e36d30", + "metadata": {}, + "source": [ + "#### Define a function named initialize_inventory that takes products as a parameter.\n", + "Inside the function, implement the code for initializing the inventory dictionary using a loop and user input." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bcad9975-71d8-4dac-9a1f-13b3167c53b9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please tell me the number of t-shirts: 10\n", + "Please tell me the number of mugs: 10\n", + "Please tell me the number of hats: 10\n", + "Please tell me the number of books: 10\n", + "Please tell me the number of keychains: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "def initialize_inventory (products):\n", + " for product in products:\n", + " inventory[product]=int(input(f'Please tell me the number of {product}s: '))\n", + " print(inventory)\n", + "\n", + "initialize_inventory(products)" + ] + }, + { + "cell_type": "markdown", + "id": "3414a8b2-68d7-4e25-be50-37b9fbb838c7", + "metadata": {}, + "source": [ + "#### Define a function named get_customer_orders that takes no parameters.\n", + "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": 4, + "id": "98e1c414-1562-4c09-ac4f-53a11071a70f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to order a t-shirt? respond yes or no yes\n", + "How many t-shirts do you want? Currently there are 10 available. 2\n", + "Do you want to order something else? no\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "total_products_ordered = {}\n", + "\n", + "def get_customer_orders():\n", + " order_is_complete = False\n", + " \n", + " while order_is_complete == False:\n", + " for product in products:\n", + " wants_product_request = input(f'Do you want to order a {product}? respond yes or no').lower()\n", + " if (wants_product_request == \"yes\"):\n", + " customer_orders.add(product)\n", + " quantity_request = input(f'How many {product}s do you want? Currently there are {inventory[product]} available.')\n", + " total_products_ordered[product] = int(quantity_request)\n", + " # elif wants_product_request == \"no\":\n", + " \n", + " wants_another_product = input(f'Do you want to order something else? respond yes or no').lower()\n", + " if (wants_another_product == \"no\"):\n", + " order_is_complete = True\n", + " break\n", + " return customer_orders\n", + " \n", + "customer_orders= get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "cfe01b73-97ff-447b-833c-414df8ca5678", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt'}\n", + "{'t-shirt': 2}\n" + ] + } + ], + "source": [ + "print(customer_orders)\n", + "print(total_products_ordered)" + ] + }, + { + "cell_type": "markdown", + "id": "625aa077-2486-469a-9bda-ce38eae376b4", + "metadata": {}, + "source": [ + "#### Define a function named update_inventory that takes customer_orders and inventory as parameters.\n", + "Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6a57559b-1f0b-41c3-95f4-db8bf6bfd8b4", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(inventory,total_products_ordered):\n", + " for product in total_products_ordered:\n", + " inventory[product]=inventory[product]-total_products_ordered[product]\n", + "\n", + "update_inventory(inventory,total_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9661863a-90f2-407e-92ac-0cc9cd17db92", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 2}\n", + "{'t-shirt': 8, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "print(total_products_ordered)\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "e77a8fe7-8375-4545-a164-d61493c6234b", + "metadata": {}, + "source": [ + "#### Define a function named calculate_order_statistics that takes customer_orders and products as parameters.\n", + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "08403420-ee0a-466c-81b4-7b99fe1e2c4e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 0.2, 'mug': 0, 'hat': 0, 'book': 0, 'keychain': 0}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_order_statistics(inventory,total_products_ordered):\n", + " statistics = {}\n", + "\n", + " for product in inventory:\n", + " if product in total_products_ordered:\n", + " statistics[product]= total_products_ordered[product]/(inventory[product]+total_products_ordered[product])\n", + " #print(f'for {product}: {total_products_ordered[product]} / {(inventory[product]+total_products_ordered[product])}')\n", + " else:\n", + " statistics[product]= 0\n", + " \n", + " return statistics\n", + "\n", + "calculate_order_statistics(inventory,total_products_ordered)" + ] + }, + { + "cell_type": "markdown", + "id": "2c9bb52b-5010-4b27-90b4-0988de3ea7c1", + "metadata": {}, + "source": [ + "#### Define a function named print_order_statistics that takes order_statistics as a parameter.\n", + "Inside the function, implement the code for printing the order statistics.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "ed75f1db-6c2a-4464-a019-c150b4e1d401", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T-shirt\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: : 0.2\n", + "\n", + "Mug\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: : 0\n", + "\n", + "Hat\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: : 0\n", + "\n", + "Book\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: : 0\n", + "\n", + "Keychain\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: : 0\n", + "\n" + ] + } + ], + "source": [ + "statistics = calculate_order_statistics(inventory,total_products_ordered)\n", + "\n", + "def print_order_statistics(total_products_ordered,statistics):\n", + " for product in inventory:\n", + " if product in total_products_ordered: \n", + " print(product.capitalize())\n", + " print(f'Total Products Ordered: {total_products_ordered[product]}')\n", + " print(f'Percentage of Products Ordered: : {statistics[product]}\\n')\n", + " else:\n", + " print(product.capitalize())\n", + " print(f'Total Products Ordered: 0')\n", + " print(f'Percentage of Products Ordered: : 0\\n')\n", + "\n", + "print_order_statistics(total_products_ordered,statistics)" + ] + }, + { + "cell_type": "markdown", + "id": "3f90a120-a0ec-4240-ad40-cb93df0676bb", + "metadata": {}, + "source": [ + "#### Define a function named print_updated_inventory that takes inventory " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "cf60cd4d-2e64-4b07-b6ef-6837ab2ccdb8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T-shirts: 8\n", + "\n", + "Mugs: 10\n", + "\n", + "Hats: 10\n", + "\n", + "Books: 10\n", + "\n", + "Keychains: 10\n", + "\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " for product in inventory:\n", + " print(f'{product.capitalize()}s: {inventory[product]}\\n')\n", + "print_updated_inventory(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cd087340-9209-49d1-a7d8-18b9722dc0c2", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.14.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..09bbfcb 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,312 @@ "\n", "\n" ] + }, + { + "cell_type": "markdown", + "id": "ce063cd0-d24a-482f-aebf-e168f0e36d30", + "metadata": {}, + "source": [ + "#### Define a function named initialize_inventory that takes products as a parameter.\n", + "Inside the function, implement the code for initializing the inventory dictionary using a loop and user input." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bcad9975-71d8-4dac-9a1f-13b3167c53b9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please tell me the number of t-shirts: 10\n", + "Please tell me the number of mugs: 10\n", + "Please tell me the number of hats: 10\n", + "Please tell me the number of books: 10\n", + "Please tell me the number of keychains: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "def initialize_inventory (products):\n", + " for product in products:\n", + " inventory[product]=int(input(f'Please tell me the number of {product}s: '))\n", + " print(inventory)\n", + "\n", + "initialize_inventory(products)" + ] + }, + { + "cell_type": "markdown", + "id": "3414a8b2-68d7-4e25-be50-37b9fbb838c7", + "metadata": {}, + "source": [ + "#### Define a function named get_customer_orders that takes no parameters.\n", + "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": 4, + "id": "98e1c414-1562-4c09-ac4f-53a11071a70f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Do you want to order a t-shirt? respond yes or no yes\n", + "How many t-shirts do you want? Currently there are 10 available. 2\n", + "Do you want to order something else? no\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "total_products_ordered = {}\n", + "\n", + "def get_customer_orders():\n", + " order_is_complete = False\n", + " \n", + " while order_is_complete == False:\n", + " for product in products:\n", + " wants_product_request = input(f'Do you want to order a {product}? respond yes or no').lower()\n", + " if (wants_product_request == \"yes\"):\n", + " customer_orders.add(product)\n", + " quantity_request = input(f'How many {product}s do you want? Currently there are {inventory[product]} available.')\n", + " total_products_ordered[product] = int(quantity_request)\n", + " # elif wants_product_request == \"no\":\n", + " \n", + " wants_another_product = input(f'Do you want to order something else? respond yes or no').lower()\n", + " if (wants_another_product == \"no\"):\n", + " order_is_complete = True\n", + " break\n", + " return customer_orders\n", + " \n", + "customer_orders= get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "cfe01b73-97ff-447b-833c-414df8ca5678", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt'}\n", + "{'t-shirt': 2}\n" + ] + } + ], + "source": [ + "print(customer_orders)\n", + "print(total_products_ordered)" + ] + }, + { + "cell_type": "markdown", + "id": "625aa077-2486-469a-9bda-ce38eae376b4", + "metadata": {}, + "source": [ + "#### Define a function named update_inventory that takes customer_orders and inventory as parameters.\n", + "Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6a57559b-1f0b-41c3-95f4-db8bf6bfd8b4", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(inventory,total_products_ordered):\n", + " for product in total_products_ordered:\n", + " inventory[product]=inventory[product]-total_products_ordered[product]\n", + "\n", + "update_inventory(inventory,total_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9661863a-90f2-407e-92ac-0cc9cd17db92", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 2}\n", + "{'t-shirt': 8, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "print(total_products_ordered)\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "e77a8fe7-8375-4545-a164-d61493c6234b", + "metadata": {}, + "source": [ + "#### Define a function named calculate_order_statistics that takes customer_orders and products as parameters.\n", + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "08403420-ee0a-466c-81b4-7b99fe1e2c4e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 0.2, 'mug': 0, 'hat': 0, 'book': 0, 'keychain': 0}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_order_statistics(inventory,total_products_ordered):\n", + " statistics = {}\n", + "\n", + " for product in inventory:\n", + " if product in total_products_ordered:\n", + " statistics[product]= total_products_ordered[product]/(inventory[product]+total_products_ordered[product])\n", + " #print(f'for {product}: {total_products_ordered[product]} / {(inventory[product]+total_products_ordered[product])}')\n", + " else:\n", + " statistics[product]= 0\n", + " \n", + " return statistics\n", + "\n", + "calculate_order_statistics(inventory,total_products_ordered)" + ] + }, + { + "cell_type": "markdown", + "id": "2c9bb52b-5010-4b27-90b4-0988de3ea7c1", + "metadata": {}, + "source": [ + "#### Define a function named print_order_statistics that takes order_statistics as a parameter.\n", + "Inside the function, implement the code for printing the order statistics.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "ed75f1db-6c2a-4464-a019-c150b4e1d401", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T-shirt\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: : 0.2\n", + "\n", + "Mug\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: : 0\n", + "\n", + "Hat\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: : 0\n", + "\n", + "Book\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: : 0\n", + "\n", + "Keychain\n", + "Total Products Ordered: 0\n", + "Percentage of Products Ordered: : 0\n", + "\n" + ] + } + ], + "source": [ + "statistics = calculate_order_statistics(inventory,total_products_ordered)\n", + "\n", + "def print_order_statistics(total_products_ordered,statistics):\n", + " for product in inventory:\n", + " if product in total_products_ordered: \n", + " print(product.capitalize())\n", + " print(f'Total Products Ordered: {total_products_ordered[product]}')\n", + " print(f'Percentage of Products Ordered: : {statistics[product]}\\n')\n", + " else:\n", + " print(product.capitalize())\n", + " print(f'Total Products Ordered: 0')\n", + " print(f'Percentage of Products Ordered: : 0\\n')\n", + "\n", + "print_order_statistics(total_products_ordered,statistics)" + ] + }, + { + "cell_type": "markdown", + "id": "3f90a120-a0ec-4240-ad40-cb93df0676bb", + "metadata": {}, + "source": [ + "#### Define a function named print_updated_inventory that takes inventory " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "cf60cd4d-2e64-4b07-b6ef-6837ab2ccdb8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T-shirts: 8\n", + "\n", + "Mugs: 10\n", + "\n", + "Hats: 10\n", + "\n", + "Books: 10\n", + "\n", + "Keychains: 10\n", + "\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " for product in inventory:\n", + " print(f'{product.capitalize()}s: {inventory[product]}\\n')\n", + "print_updated_inventory(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cd087340-9209-49d1-a7d8-18b9722dc0c2", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +367,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.5" } }, "nbformat": 4,