diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..a30637e --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,190 @@ +{ + "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": 7, + "id": "1f13f330-4a86-4801-bbd1-86b2277627ac", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "please enter the quantity of each product available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 8\n", + "mug: 1\n", + "hat: 5\n", + "book: 6\n", + "keychain: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "inventory availible: {'t-shirt': 8, 'mug': 1, 'hat': 5, 'book': 6, 'keychain': 7}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "which product do you want to buy: mug\n", + "do you want to buy another product? (y/n): y\n", + "which product do you want to buy: hat\n", + "do you want to buy another product? (y/n): y\n", + "which product do you want to buy: book\n", + "do you want to buy another product? (y/n): n\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered:3\n", + "Percentage of Products Ordered:60.00%\n", + "updated inventory: {'t-shirt': 8, 'mug': 0, 'hat': 4, 'book': 5, 'keychain': 7}\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "\n", + "def initialize_inventory(products_list:list,ini_inventory={}) -> dict:\n", + " print(\"please enter the quantity of each product available.\")\n", + " for i in products:\n", + " quantity=int(input(i+\":\"))\n", + " while quantity<0:\n", + " quantity=int(input(\"please enter a new number that is not under 0:\"))\n", + " ini_inventory[i]=quantity\n", + " return ini_inventory\n", + " \n", + "def get_customer_orders():\n", + " customer_orders=set()\n", + " x=\"y\"\n", + " while x==\"y\":\n", + " customer_orders.add(input(\"which product do you want to buy:\"))\n", + " x=input(\"do you want to buy another product? (y/n):\")\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders,inventory):\n", + " for i in customer_orders:\n", + " if inventory[i]!= 0:\n", + " inventory[i]-=1\n", + "\n", + "def calculate_order_statistics(customer_orders,products):\n", + " total_products_ordered=len(customer_orders)\n", + " percentage_ordered=len(customer_orders)/len(products)\n", + " order_status=(total_products_ordered,percentage_ordered)\n", + " return order_status\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " print(f\"Order Statistics:\\nTotal Products Ordered:{order_statistics[0]}\\nPercentage of Products Ordered:{order_statistics[1]:.2%}\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"updated inventory:\",inventory)\n", + "\n", + "inventory=initialize_inventory(products)\n", + "print(\"inventory availible:\",inventory)\n", + "\n", + "customer_orders=get_customer_orders()\n", + "update_inventory(customer_orders,inventory)\n", + "\n", + "print_order_statistics(calculate_order_statistics(customer_orders,products))\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61930678-7cbb-4d43-bc4d-fa39d5af463a", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1856cdb7-3759-4b85-b67d-f68d8b016bb3", + "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.14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..a30637e 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,127 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "1f13f330-4a86-4801-bbd1-86b2277627ac", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "please enter the quantity of each product available.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "t-shirt: 8\n", + "mug: 1\n", + "hat: 5\n", + "book: 6\n", + "keychain: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "inventory availible: {'t-shirt': 8, 'mug': 1, 'hat': 5, 'book': 6, 'keychain': 7}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "which product do you want to buy: mug\n", + "do you want to buy another product? (y/n): y\n", + "which product do you want to buy: hat\n", + "do you want to buy another product? (y/n): y\n", + "which product do you want to buy: book\n", + "do you want to buy another product? (y/n): n\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered:3\n", + "Percentage of Products Ordered:60.00%\n", + "updated inventory: {'t-shirt': 8, 'mug': 0, 'hat': 4, 'book': 5, 'keychain': 7}\n" + ] + } + ], + "source": [ + "products=[\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "\n", + "def initialize_inventory(products_list:list,ini_inventory={}) -> dict:\n", + " print(\"please enter the quantity of each product available.\")\n", + " for i in products:\n", + " quantity=int(input(i+\":\"))\n", + " while quantity<0:\n", + " quantity=int(input(\"please enter a new number that is not under 0:\"))\n", + " ini_inventory[i]=quantity\n", + " return ini_inventory\n", + " \n", + "def get_customer_orders():\n", + " customer_orders=set()\n", + " x=\"y\"\n", + " while x==\"y\":\n", + " customer_orders.add(input(\"which product do you want to buy:\"))\n", + " x=input(\"do you want to buy another product? (y/n):\")\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders,inventory):\n", + " for i in customer_orders:\n", + " if inventory[i]!= 0:\n", + " inventory[i]-=1\n", + "\n", + "def calculate_order_statistics(customer_orders,products):\n", + " total_products_ordered=len(customer_orders)\n", + " percentage_ordered=len(customer_orders)/len(products)\n", + " order_status=(total_products_ordered,percentage_ordered)\n", + " return order_status\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " print(f\"Order Statistics:\\nTotal Products Ordered:{order_statistics[0]}\\nPercentage of Products Ordered:{order_statistics[1]:.2%}\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"updated inventory:\",inventory)\n", + "\n", + "inventory=initialize_inventory(products)\n", + "print(\"inventory availible:\",inventory)\n", + "\n", + "customer_orders=get_customer_orders()\n", + "update_inventory(customer_orders,inventory)\n", + "\n", + "print_order_statistics(calculate_order_statistics(customer_orders,products))\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61930678-7cbb-4d43-bc4d-fa39d5af463a", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1856cdb7-3759-4b85-b67d-f68d8b016bb3", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +182,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.14" } }, "nbformat": 4,