diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..98a30c3 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -20,34 +20,283 @@ "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", + "Hints for functions:\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", + "- 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", - "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" + ] + }, + { + "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_list = ['mug', 'hat', 'keychain', 'book', 't-shirt']\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", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products: \n", + " qty = int(input(f\"Enter quantity for {product}: \")) \n", + " inventory[product] = qty\n", + " return inventory" + ] + }, + { + "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", - "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 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" + ] + }, + { + "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": null, + "id": "9f784305", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", + "customer_orders = ('mug', 't-shirt')\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", + "def update_inventory(customer_orders, inventory):\n", + " \n", + " for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item]-=1\n", "\n", - "7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + " return inventory" + ] + }, + { + "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": 45, + "id": "5489e00c", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {'mug': 1, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 5}\n", + "customer_orders = ('mug', 't-shirt')\n", "\n", - "Hints for functions:\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" + ] + }, + { + "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": 75, + "id": "61448a34", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, inventory):\n", + " total_products_ordered = len(set(customer_orders))\n", + " percentage_ordered = (total_products_ordered / len(inventory)) * 100\n", + " return total_products_ordered, percentage_ordered\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", + "def print_order_statistics(order_statistics):\n", + " print(f\"Total unique products ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of unique products ordered: {order_statistics[1]:.2f}%\")\n" + ] + }, + { + "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": null, + "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", + " for product, qty in inventory.items():\n", + " print (f\"{product}: {qty}\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "33520633", + "metadata": {}, + "source": [ + "7. Calling Functions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "606189c9", + "metadata": {}, + "outputs": [], + "source": [ + "# Correct function call\n", + "inventory = initialize_inventory(inventory_list)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d390065f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thanks for your purchase.\n" + ] + } + ], + "source": [ + "get_customers_orders()\n", + "print(\"Thanks for your purchase.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1c25c92", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'mug': 0, 'hat': 2, 'keychain': 3, 'book': 4, 't-shirt': 4}" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9fc59c64", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 40.0)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "calculate_order_statistics(customer_orders, inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eaf3f015", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total unique products ordered: 2\n", + "Percentage of unique products ordered: 40.00%\n" + ] + } + ], + "source": [ + "# Calculating statistics based on your data\n", + "order_stats = calculate_order_statistics(customer_orders, inventory)\n", "\n", - "\n" + "# Printing those statistics\n", + "print_order_statistics(order_stats)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +310,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,