From 3f3b1c2821aea9b60ad15b21d8ee7d2eadb4e230 Mon Sep 17 00:00:00 2001 From: mrtimN Date: Tue, 7 Jan 2025 15:41:29 +0100 Subject: [PATCH 1/2] Lab Python Functions --- .../lab-python-functions-checkpoint.ipynb | 262 ++++++++++++++++++ lab-python-functions.ipynb | 199 ++++++++++++- 2 files changed, 458 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..7614d5f --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,262 @@ +{ + "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": 80, + "id": "3c7647ca-d446-4b17-9916-799f1f9f0fd5", + "metadata": {}, + "outputs": [], + "source": [ + "test_products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " # loop through all products and add quantity\n", + " for product in products:\n", + " inventory[product] = int(input(\"Please enter the quantity of \" + product + \":\"))\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "9b4a0f8e-091f-4518-9b99-13c6f699ba01", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " orders = set()\n", + " answer = True\n", + " while answer == True:\n", + " orders.add(input(\"Please enter the name of your desired product: \"))\n", + " another = input(\"Do you want to add another product? Answer with 'yes' or 'no': \")\n", + " if another == \"yes\":\n", + " answer = True\n", + " elif another == \"no\":\n", + " answer = False\n", + " return orders" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "741dd52d-ab62-4925-b169-cfa824e8c3fe", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for key in customer_orders:\n", + " inventory[key] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "6a27f45e-fcb4-49e0-8c1f-6b4ee418cea3", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " total_products_available = len(products)\n", + " percentage_ordered = total_products_ordered/total_products_available*100\n", + " return (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "4da75305-3308-4b17-9cea-3f1c5b5bc0d5", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"Order statistics: \")\n", + " print(\"Total products ordered: \" + str(order_statistics[0]))\n", + " print(\"Percentage of Products ordered: \" + str(order_statistics[1]) + \"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "f9ca7df0-2fe1-4e39-a4a7-606425ff4db7", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " for key in inventory:\n", + " print(key + \": \" + str(inventory[key]))" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "b73ad429-c63a-4fd1-9388-d0589e71127c", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt: 1\n", + "Please enter the quantity of mug: 2\n", + "Please enter the quantity of hat: 3\n", + "Please enter the quantity of book: 4\n", + "Please enter the quantity of keychain: 5\n" + ] + } + ], + "source": [ + "test_inventory = initialize_inventory(test_products)" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "d20b5f5a-2d7d-4502-8060-ff39afadb439", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your desired product: mug\n", + "Do you want to add another product? Answer with 'yes' or 'no': yes\n", + "Please enter the name of your desired product: hat\n", + "Do you want to add another product? Answer with 'yes' or 'no': yes\n", + "Please enter the name of your desired product: book\n", + "Do you want to add another product? Answer with 'yes' or 'no': no\n" + ] + } + ], + "source": [ + "test_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "b26e6e90-670a-475c-a73c-6eebedbefe4e", + "metadata": {}, + "outputs": [], + "source": [ + "update_inventory(test_orders, test_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "203b87ca-378a-4e2a-9fd5-d8a6f6ded180", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: \n", + "Total products ordered: 3\n", + "Percentage of Products ordered: 60.0%\n" + ] + } + ], + "source": [ + "print_order_statistics(calculate_order_statistics(test_orders, test))" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "3e0147e2-e2e3-4f74-b892-d656fbe79123", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 1\n", + "mug: 1\n", + "hat: 2\n", + "book: 3\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "print_updated_inventory(test_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5497eb81-6303-495c-9f4c-91f38a3f394a", + "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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..7614d5f 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,206 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "3c7647ca-d446-4b17-9916-799f1f9f0fd5", + "metadata": {}, + "outputs": [], + "source": [ + "test_products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " # loop through all products and add quantity\n", + " for product in products:\n", + " inventory[product] = int(input(\"Please enter the quantity of \" + product + \":\"))\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "9b4a0f8e-091f-4518-9b99-13c6f699ba01", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " orders = set()\n", + " answer = True\n", + " while answer == True:\n", + " orders.add(input(\"Please enter the name of your desired product: \"))\n", + " another = input(\"Do you want to add another product? Answer with 'yes' or 'no': \")\n", + " if another == \"yes\":\n", + " answer = True\n", + " elif another == \"no\":\n", + " answer = False\n", + " return orders" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "741dd52d-ab62-4925-b169-cfa824e8c3fe", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for key in customer_orders:\n", + " inventory[key] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "6a27f45e-fcb4-49e0-8c1f-6b4ee418cea3", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " total_products_available = len(products)\n", + " percentage_ordered = total_products_ordered/total_products_available*100\n", + " return (total_products_ordered, percentage_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "4da75305-3308-4b17-9cea-3f1c5b5bc0d5", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"Order statistics: \")\n", + " print(\"Total products ordered: \" + str(order_statistics[0]))\n", + " print(\"Percentage of Products ordered: \" + str(order_statistics[1]) + \"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "f9ca7df0-2fe1-4e39-a4a7-606425ff4db7", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " for key in inventory:\n", + " print(key + \": \" + str(inventory[key]))" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "b73ad429-c63a-4fd1-9388-d0589e71127c", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity of t-shirt: 1\n", + "Please enter the quantity of mug: 2\n", + "Please enter the quantity of hat: 3\n", + "Please enter the quantity of book: 4\n", + "Please enter the quantity of keychain: 5\n" + ] + } + ], + "source": [ + "test_inventory = initialize_inventory(test_products)" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "d20b5f5a-2d7d-4502-8060-ff39afadb439", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your desired product: mug\n", + "Do you want to add another product? Answer with 'yes' or 'no': yes\n", + "Please enter the name of your desired product: hat\n", + "Do you want to add another product? Answer with 'yes' or 'no': yes\n", + "Please enter the name of your desired product: book\n", + "Do you want to add another product? Answer with 'yes' or 'no': no\n" + ] + } + ], + "source": [ + "test_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "b26e6e90-670a-475c-a73c-6eebedbefe4e", + "metadata": {}, + "outputs": [], + "source": [ + "update_inventory(test_orders, test_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "203b87ca-378a-4e2a-9fd5-d8a6f6ded180", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: \n", + "Total products ordered: 3\n", + "Percentage of Products ordered: 60.0%\n" + ] + } + ], + "source": [ + "print_order_statistics(calculate_order_statistics(test_orders, test))" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "3e0147e2-e2e3-4f74-b892-d656fbe79123", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 1\n", + "mug: 1\n", + "hat: 2\n", + "book: 3\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "print_updated_inventory(test_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5497eb81-6303-495c-9f4c-91f38a3f394a", + "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 +254,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, From b3dd6d098e51729300b9debdc30538d7d28b254b Mon Sep 17 00:00:00 2001 From: mrtimN Date: Tue, 7 Jan 2025 15:45:47 +0100 Subject: [PATCH 2/2] Correction --- .../lab-python-functions-checkpoint.ipynb | 27 ++++++++++--------- lab-python-functions.ipynb | 27 ++++++++++--------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb index 7614d5f..7923879 100644 --- a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -46,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 2, "id": "3c7647ca-d446-4b17-9916-799f1f9f0fd5", "metadata": {}, "outputs": [], @@ -63,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 4, "id": "9b4a0f8e-091f-4518-9b99-13c6f699ba01", "metadata": {}, "outputs": [], @@ -83,19 +83,20 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 6, "id": "741dd52d-ab62-4925-b169-cfa824e8c3fe", "metadata": {}, "outputs": [], "source": [ "def update_inventory(customer_orders, inventory):\n", " for key in customer_orders:\n", - " inventory[key] -= 1" + " if key in inventory:\n", + " inventory[key] -= 1" ] }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 8, "id": "6a27f45e-fcb4-49e0-8c1f-6b4ee418cea3", "metadata": {}, "outputs": [], @@ -109,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 10, "id": "4da75305-3308-4b17-9cea-3f1c5b5bc0d5", "metadata": {}, "outputs": [], @@ -122,7 +123,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 12, "id": "f9ca7df0-2fe1-4e39-a4a7-606425ff4db7", "metadata": {}, "outputs": [], @@ -134,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 14, "id": "b73ad429-c63a-4fd1-9388-d0589e71127c", "metadata": {}, "outputs": [ @@ -156,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 16, "id": "d20b5f5a-2d7d-4502-8060-ff39afadb439", "metadata": {}, "outputs": [ @@ -179,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 18, "id": "b26e6e90-670a-475c-a73c-6eebedbefe4e", "metadata": {}, "outputs": [], @@ -189,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 20, "id": "203b87ca-378a-4e2a-9fd5-d8a6f6ded180", "metadata": {}, "outputs": [ @@ -204,12 +205,12 @@ } ], "source": [ - "print_order_statistics(calculate_order_statistics(test_orders, test))" + "print_order_statistics(calculate_order_statistics(test_orders, test_products))" ] }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 22, "id": "3e0147e2-e2e3-4f74-b892-d656fbe79123", "metadata": {}, "outputs": [ diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7614d5f..7923879 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -46,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 2, "id": "3c7647ca-d446-4b17-9916-799f1f9f0fd5", "metadata": {}, "outputs": [], @@ -63,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 4, "id": "9b4a0f8e-091f-4518-9b99-13c6f699ba01", "metadata": {}, "outputs": [], @@ -83,19 +83,20 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 6, "id": "741dd52d-ab62-4925-b169-cfa824e8c3fe", "metadata": {}, "outputs": [], "source": [ "def update_inventory(customer_orders, inventory):\n", " for key in customer_orders:\n", - " inventory[key] -= 1" + " if key in inventory:\n", + " inventory[key] -= 1" ] }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 8, "id": "6a27f45e-fcb4-49e0-8c1f-6b4ee418cea3", "metadata": {}, "outputs": [], @@ -109,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 10, "id": "4da75305-3308-4b17-9cea-3f1c5b5bc0d5", "metadata": {}, "outputs": [], @@ -122,7 +123,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 12, "id": "f9ca7df0-2fe1-4e39-a4a7-606425ff4db7", "metadata": {}, "outputs": [], @@ -134,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 14, "id": "b73ad429-c63a-4fd1-9388-d0589e71127c", "metadata": {}, "outputs": [ @@ -156,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 16, "id": "d20b5f5a-2d7d-4502-8060-ff39afadb439", "metadata": {}, "outputs": [ @@ -179,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 18, "id": "b26e6e90-670a-475c-a73c-6eebedbefe4e", "metadata": {}, "outputs": [], @@ -189,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 20, "id": "203b87ca-378a-4e2a-9fd5-d8a6f6ded180", "metadata": {}, "outputs": [ @@ -204,12 +205,12 @@ } ], "source": [ - "print_order_statistics(calculate_order_statistics(test_orders, test))" + "print_order_statistics(calculate_order_statistics(test_orders, test_products))" ] }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 22, "id": "3e0147e2-e2e3-4f74-b892-d656fbe79123", "metadata": {}, "outputs": [