diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..2abb2c4 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -20,34 +20,275 @@ "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", + "7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\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", + "Hints for functions:\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", + "- 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", - "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" + ] + }, + { + "cell_type": "markdown", + "id": "1b7f10d7", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba8a897e", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {'mug':0, 'hat':0, 'keychain':0, 'book':0, 't-shirt':0}\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", + "def initialize_inventory(products):\n", + " for item in inventory:\n", + " products = input (\"Enter a value\")\n", + " if products in inventory:\n", + " value = (int(input(\"Enter a value\")))\n", + " inventory[products]=value\n", + " \n", + " \n", + " return products\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" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "id": "1df42982", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n" + ] + } + ], + "source": [ + "products = inventory\n", + "initialize_inventory (products)\n", + "print(products)\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" + ] + }, + { + "cell_type": "markdown", + "id": "6610d470", + "metadata": {}, + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4300e4a8", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()\n", "\n", + "def get_customers_orders(): \n", + " \n", + " continue_buying = \"Yes\"\n", + " \n", + " while continue_buying == \"Yes\":\n", + " product = input(\"What do you want to buy?: \")\n", + " customer_orders.add(product)\n", + " continue_buying = input(\"Do you want to continue buying? (Yes/No): \")\n", + " \n", + " return customer_orders\n", + " \n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 162, + "id": "70b11c12", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 't-shirt'}\n", + "Thanks for your purchase.\n" + ] + } + ], + "source": [ + "get_customers_orders()\n", + "print(customer_orders)\n", + "print(\"Thanks for your purchase.\")" + ] + }, + { + "cell_type": "markdown", + "id": "20638e0d", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "9f784305", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}\n" + ] + } + ], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", + "customer_orders = ('mug', 't-shirt')\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \n", + " for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item]-=1\n", + "\n", + " return customer_orders, inventory\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "print(inventory)\n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "b7a40557", + "metadata": {}, + "source": [ + "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 (Number of different products)). The function should return these values." + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "5489e00c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 40.0)" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", + "customer_orders = ('mug', 't-shirt')\n", + "\n", + "def calculate_order_statistics(customer_orders, inventory):\n", + " total_products_ordered = len(customer_orders)\n", + " total_products_inventory = len(inventory)\n", + " percent_unique_products = (total_products_ordered/total_products_inventory)*100\n", + " \n", + " return total_products_ordered, percent_unique_products\n", + "\n", + "calculate_order_statistics(customer_orders, inventory)\n" + ] + }, + { + "cell_type": "markdown", + "id": "35a81169", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "61448a34", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " return order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "69891f1d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total_products_ordered = order_statistics\n", + "print_order_statistics(total_products_ordered)" + ] + }, + { + "cell_type": "markdown", + "id": "9dc024e1", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "87f8554a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " return inventory\n", + "print(inventory)\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +302,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,