diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..4f17aea --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,309 @@ +{ + "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": "514a8f03-d3b7-45a9-93e0-74390401228a", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"tshirts\", \"mugs\", \"hats\", \"books\", \"keychains\"]\n", + "inventory = {}\n", + "\n", + "def initialize_inventory(products):\n", + " for items in products:\n", + " inventory[items] = int(input(f\"Amount of {items} available:\"))\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "34d37e1f-d7c6-46f0-a8d8-24f3edda6f62", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Amount of tshirts available: 5\n", + "Amount of mugs available: 5\n", + "Amount of hats available: 5\n", + "Amount of books available: 5\n", + "Amount of keychains available: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "{'tshirts': 5, 'mugs': 5, 'hats': 5, 'books': 5, 'keychains': 5}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "988ef877-0568-4c1c-86a9-469a8e55f908", + "metadata": {}, + "outputs": [], + "source": [ + "customer_order = set()\n", + "def get_customer_order():\n", + " for product in [\"Product#1\", \"Product#2\", \"Product#3\"]:\n", + " new_order = input(f\"Enter the name of {product}: \")\n", + " customer_order.add(new_order)\n", + " return customer_order" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "440ecb01-7798-469e-b060-47e0007343d9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of Product#1: mugs\n", + "Enter the name of Product#2: hats\n", + "Enter the name of Product#3: books\n" + ] + }, + { + "data": { + "text/plain": [ + "{'books', 'hats', 'mugs'}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_customer_order()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "0a0cf2d8-8709-46b3-9e7b-246922a265a7", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_order, inventory):\n", + " for item in customer_order:\n", + " inventory[item] -= 1\n", + " return print(\"New Updated Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "185fd629-b842-4964-b036-1473fcbb43cd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "New Updated Inventory: {'tshirts': 5, 'mugs': 4, 'hats': 4, 'books': 4, 'keychains': 5}\n" + ] + } + ], + "source": [ + "update_inventory(customer_order, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "b4148b5a-1227-42f8-9449-7fe0c78b91f5", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_order, inventory):\n", + " total_available_products = sum(inventory.values())\n", + " total_products_ordered = len(customer_order)\n", + " percentage_ordered = (total_products_ordered / total_available_products)*100\n", + " return percentage_ordered, total_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "c468a698-769d-4e74-bee7-84d3089b2d6c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(13.636363636363635, 3)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_order_statistics(customer_order, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "ad1b33e8-d26f-4e01-8f48-06cc658c7859", + "metadata": {}, + "outputs": [], + "source": [ + "percentage_ordered, total_products_ordered = calculate_order_statistics(customer_order, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "afce9c4c-1431-4337-b76d-3408b3d1019c", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"Total Products Ordered: \", total_products_ordered)\n", + " print(\"Percentage of Products Ordered:\", percentage_ordered,\"%\")\n", + " return percentage_ordered, total_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "543f3252-5529-4972-a9b9-1829854c80d3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 13.636363636363635 %\n" + ] + }, + { + "data": { + "text/plain": [ + "(13.636363636363635, 3)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "344b0ca2-5265-479d-a5c5-14fb0f3eefd8", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(f\"This is the current updated inventory {inventory}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "693e609d-5d95-4abf-8cb0-9b158adaa417", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the current updated inventory {'tshirts': 5, 'mugs': 4, 'hats': 4, 'books': 4, 'keychains': 5}\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + } + ], + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..4f17aea 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,246 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "514a8f03-d3b7-45a9-93e0-74390401228a", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"tshirts\", \"mugs\", \"hats\", \"books\", \"keychains\"]\n", + "inventory = {}\n", + "\n", + "def initialize_inventory(products):\n", + " for items in products:\n", + " inventory[items] = int(input(f\"Amount of {items} available:\"))\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "34d37e1f-d7c6-46f0-a8d8-24f3edda6f62", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Amount of tshirts available: 5\n", + "Amount of mugs available: 5\n", + "Amount of hats available: 5\n", + "Amount of books available: 5\n", + "Amount of keychains available: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "{'tshirts': 5, 'mugs': 5, 'hats': 5, 'books': 5, 'keychains': 5}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "988ef877-0568-4c1c-86a9-469a8e55f908", + "metadata": {}, + "outputs": [], + "source": [ + "customer_order = set()\n", + "def get_customer_order():\n", + " for product in [\"Product#1\", \"Product#2\", \"Product#3\"]:\n", + " new_order = input(f\"Enter the name of {product}: \")\n", + " customer_order.add(new_order)\n", + " return customer_order" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "440ecb01-7798-469e-b060-47e0007343d9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of Product#1: mugs\n", + "Enter the name of Product#2: hats\n", + "Enter the name of Product#3: books\n" + ] + }, + { + "data": { + "text/plain": [ + "{'books', 'hats', 'mugs'}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_customer_order()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "0a0cf2d8-8709-46b3-9e7b-246922a265a7", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_order, inventory):\n", + " for item in customer_order:\n", + " inventory[item] -= 1\n", + " return print(\"New Updated Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "185fd629-b842-4964-b036-1473fcbb43cd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "New Updated Inventory: {'tshirts': 5, 'mugs': 4, 'hats': 4, 'books': 4, 'keychains': 5}\n" + ] + } + ], + "source": [ + "update_inventory(customer_order, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "b4148b5a-1227-42f8-9449-7fe0c78b91f5", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_order, inventory):\n", + " total_available_products = sum(inventory.values())\n", + " total_products_ordered = len(customer_order)\n", + " percentage_ordered = (total_products_ordered / total_available_products)*100\n", + " return percentage_ordered, total_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "c468a698-769d-4e74-bee7-84d3089b2d6c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(13.636363636363635, 3)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_order_statistics(customer_order, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "ad1b33e8-d26f-4e01-8f48-06cc658c7859", + "metadata": {}, + "outputs": [], + "source": [ + "percentage_ordered, total_products_ordered = calculate_order_statistics(customer_order, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "afce9c4c-1431-4337-b76d-3408b3d1019c", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"Total Products Ordered: \", total_products_ordered)\n", + " print(\"Percentage of Products Ordered:\", percentage_ordered,\"%\")\n", + " return percentage_ordered, total_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "543f3252-5529-4972-a9b9-1829854c80d3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 13.636363636363635 %\n" + ] + }, + { + "data": { + "text/plain": [ + "(13.636363636363635, 3)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "344b0ca2-5265-479d-a5c5-14fb0f3eefd8", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(f\"This is the current updated inventory {inventory}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "693e609d-5d95-4abf-8cb0-9b158adaa417", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is the current updated inventory {'tshirts': 5, 'mugs': 4, 'hats': 4, 'books': 4, 'keychains': 5}\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] } ], "metadata": { @@ -61,7 +301,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,