diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..d585b95 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,316 @@ +{ + "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": 11, + "id": "00f3e9ad-3228-4bf2-b321-336abf7da8c6", + "metadata": {}, + "outputs": [], + "source": [ + "# initializing the inventory dictionary \n", + "\n", + "def initialize_inventory(products): \n", + " '''\n", + " take a product list as parameter\n", + " ask user input of product quantity\n", + " return a inventory dictionary and a message to show customers what products we currently have\n", + " '''\n", + " inventory = {}\n", + " for p in products:\n", + " inventory[p] = int(input(f\"Please enter the quantity for product {p}:\" ))\n", + " \n", + " print(\"\\nWe have following products:\")\n", + " for count,product in list(enumerate(products,start = 1)):\n", + " print(count,product.upper())\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f9e0d5d6-9805-4403-89a2-78b3052dda4e", + "metadata": {}, + "outputs": [], + "source": [ + "# create a customer order set\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " '''\n", + " take no arguments\n", + " prompting the user to enter the product names\n", + " return a customer orders set \n", + " '''\n", + " customer_orders = set()\n", + " print(\"\\nType the products and you want to order (type 'done' to finish).\")\n", + "\n", + " while True:\n", + " choice = input(\"\\nEnter product name: \") \n", + " if choice.lower() == \"done\":\n", + " break\n", + " else:\n", + " if choice not in inventory.keys():\n", + " print(\"Product does not exist in our inventory.Please re-enter.\")\n", + " continue\n", + " else:\n", + " customer_orders.add(choice) \n", + " \n", + " return customer_orders " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "16e4a719-f046-48dc-9083-62746e705ed1", + "metadata": {}, + "outputs": [], + "source": [ + "# create a update inventory \n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " '''\n", + " takes customer orders, initial inventory as parameters,\n", + " update inventory based on customer order\n", + " '''\n", + " for i in customer_orders:\n", + " inventory[i] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8cf10e0b-28a2-4949-8af9-9e38f1e7bd05", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def calculate_order_statstics (customer_orders, products):\n", + " '''\n", + " takes customer order set and product list as parameters\n", + " calculate the total products ordered and percentage of products ordered\n", + " return their values\n", + " '''\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_products_ordered = total_products_ordered/len(products)*100\n", + " return total_products_ordered,percentage_of_products_ordered # tuple\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "e8617fc7-b42b-4e65-9a93-b0d5008761a1", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def print_order_statistics(order_statistics):\n", + " '''\n", + " takes order statistics tuple as parameter\n", + " return a message to show values \n", + " ''' \n", + " print(\"\\nThe total number of unique products customer ordered is: \", order_statistics[0])\n", + " print(\"\\nThe percentage of products ordered is: \", order_statistics[1])\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "63791294-4df5-4bcb-a276-841efa4c76ac", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def print_updated_inventory(updated_inventory):\n", + " '''\n", + " takes inventory dictionary as parameter\n", + " print out updated inventory\n", + " '''\n", + " print(\"\\nUpdated Inventory as follows:\")\n", + " for k,v in inventory.items():\n", + " print(k,\" \",v)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "5c74cb2a-3257-4e9e-a7f6-d800f0f5cc7f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity for product t-shirt: 3\n", + "Please enter the quantity for product mug: 3\n", + "Please enter the quantity for product hat: 3\n", + "Please enter the quantity for product book: 3\n", + "Please enter the quantity for product keychain: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "We have following products:\n", + "1 T-SHIRT\n", + "2 MUG\n", + "3 HAT\n", + "4 BOOK\n", + "5 KEYCHAIN\n", + "\n", + "Type the products and you want to order (type 'done' to finish).\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Enter product name: m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product does not exist in our inventory.Please re-enter.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Enter product name: m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product does not exist in our inventory.Please re-enter.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Enter product name: hat\n", + "\n", + "Enter product name: mug\n", + "\n", + "Enter product name: DONE\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "The total number of unique products customer ordered is: 2\n", + "\n", + "The percentage of products ordered is: 40.0\n", + "\n", + "Updated Inventory as follows:\n", + "t-shirt 3\n", + "mug 2\n", + "hat 2\n", + "book 3\n", + "keychain 3\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "order_statistics = calculate_order_statstics(customer_orders,products) \n", + "print_order_statistics(order_statistics)\n", + "\n", + "updated_inventory = update_inventory(customer_orders,inventory)\n", + "print_updated_inventory(update_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d275ecd6-2ed0-43d6-b197-0ff59e42eff5", + "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.10.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..d585b95 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,253 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "00f3e9ad-3228-4bf2-b321-336abf7da8c6", + "metadata": {}, + "outputs": [], + "source": [ + "# initializing the inventory dictionary \n", + "\n", + "def initialize_inventory(products): \n", + " '''\n", + " take a product list as parameter\n", + " ask user input of product quantity\n", + " return a inventory dictionary and a message to show customers what products we currently have\n", + " '''\n", + " inventory = {}\n", + " for p in products:\n", + " inventory[p] = int(input(f\"Please enter the quantity for product {p}:\" ))\n", + " \n", + " print(\"\\nWe have following products:\")\n", + " for count,product in list(enumerate(products,start = 1)):\n", + " print(count,product.upper())\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f9e0d5d6-9805-4403-89a2-78b3052dda4e", + "metadata": {}, + "outputs": [], + "source": [ + "# create a customer order set\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " '''\n", + " take no arguments\n", + " prompting the user to enter the product names\n", + " return a customer orders set \n", + " '''\n", + " customer_orders = set()\n", + " print(\"\\nType the products and you want to order (type 'done' to finish).\")\n", + "\n", + " while True:\n", + " choice = input(\"\\nEnter product name: \") \n", + " if choice.lower() == \"done\":\n", + " break\n", + " else:\n", + " if choice not in inventory.keys():\n", + " print(\"Product does not exist in our inventory.Please re-enter.\")\n", + " continue\n", + " else:\n", + " customer_orders.add(choice) \n", + " \n", + " return customer_orders " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "16e4a719-f046-48dc-9083-62746e705ed1", + "metadata": {}, + "outputs": [], + "source": [ + "# create a update inventory \n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " '''\n", + " takes customer orders, initial inventory as parameters,\n", + " update inventory based on customer order\n", + " '''\n", + " for i in customer_orders:\n", + " inventory[i] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8cf10e0b-28a2-4949-8af9-9e38f1e7bd05", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def calculate_order_statstics (customer_orders, products):\n", + " '''\n", + " takes customer order set and product list as parameters\n", + " calculate the total products ordered and percentage of products ordered\n", + " return their values\n", + " '''\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_products_ordered = total_products_ordered/len(products)*100\n", + " return total_products_ordered,percentage_of_products_ordered # tuple\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "e8617fc7-b42b-4e65-9a93-b0d5008761a1", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def print_order_statistics(order_statistics):\n", + " '''\n", + " takes order statistics tuple as parameter\n", + " return a message to show values \n", + " ''' \n", + " print(\"\\nThe total number of unique products customer ordered is: \", order_statistics[0])\n", + " print(\"\\nThe percentage of products ordered is: \", order_statistics[1])\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "63791294-4df5-4bcb-a276-841efa4c76ac", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def print_updated_inventory(updated_inventory):\n", + " '''\n", + " takes inventory dictionary as parameter\n", + " print out updated inventory\n", + " '''\n", + " print(\"\\nUpdated Inventory as follows:\")\n", + " for k,v in inventory.items():\n", + " print(k,\" \",v)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "5c74cb2a-3257-4e9e-a7f6-d800f0f5cc7f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the quantity for product t-shirt: 3\n", + "Please enter the quantity for product mug: 3\n", + "Please enter the quantity for product hat: 3\n", + "Please enter the quantity for product book: 3\n", + "Please enter the quantity for product keychain: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "We have following products:\n", + "1 T-SHIRT\n", + "2 MUG\n", + "3 HAT\n", + "4 BOOK\n", + "5 KEYCHAIN\n", + "\n", + "Type the products and you want to order (type 'done' to finish).\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Enter product name: m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product does not exist in our inventory.Please re-enter.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Enter product name: m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product does not exist in our inventory.Please re-enter.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "\n", + "Enter product name: hat\n", + "\n", + "Enter product name: mug\n", + "\n", + "Enter product name: DONE\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "The total number of unique products customer ordered is: 2\n", + "\n", + "The percentage of products ordered is: 40.0\n", + "\n", + "Updated Inventory as follows:\n", + "t-shirt 3\n", + "mug 2\n", + "hat 2\n", + "book 3\n", + "keychain 3\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "order_statistics = calculate_order_statstics(customer_orders,products) \n", + "print_order_statistics(order_statistics)\n", + "\n", + "updated_inventory = update_inventory(customer_orders,inventory)\n", + "print_updated_inventory(update_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d275ecd6-2ed0-43d6-b197-0ff59e42eff5", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +308,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.10" } }, "nbformat": 4,