diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..5c79c70 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,350 @@ +{ + "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": 2, + "id": "a0e25348-319a-4b12-95fe-5f609482646f", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Initialize inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "be4e90b8-e93d-4b6b-941c-4a1fee50e47b", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Get customer orders\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter a product to order (or 'done' to finish): \").strip()\n", + " if order.lower() == \"done\":\n", + " break\n", + " customer_orders.add(order)\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "053743fe-45b6-4ec7-b001-6c4be5cee5a3", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory and inventory[order] > 0:\n", + " inventory[order] -= 1\n", + " else:\n", + " print(f\"Sorry, {order} is not available or out of stock.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "8bc7c17b-91db-4ab6-bf6e-df64e6aee751", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique = (len(customer_orders) / len(products)) * 100\n", + " return total_products_ordered, percentage_unique" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "e8570722-9eae-494c-bcc1-2f9936ef4b10", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 5: Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total unique products ordered: {total}\")\n", + " print(f\"Percentage of available products ordered: {percentage:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "bb510941-b885-4e2c-bcb9-bbcd7e658a17", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 6: Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "46e4aca3-d5a7-4e28-b546-585f6191f017", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for apple: 20\n", + "Enter quantity for banana: 12\n", + "Enter quantity for orange: 24\n", + "Enter quantity for grape: 30\n", + "Enter a product to order (or 'done' to finish): bnana\n", + "Enter a product to order (or 'done' to finish): orange\n", + "Enter a product to order (or 'done' to finish): papaya\n", + "Enter a product to order (or 'done' to finish): grape\n", + "Enter a product to order (or 'done' to finish): pple\n", + "Enter a product to order (or 'done' to finish): done\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'update_inventory' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[8]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[32m 3\u001b[39m inventory = initialize_inventory(products)\n\u001b[32m 4\u001b[39m customer_orders = get_customer_orders()\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m inventory = \u001b[43mupdate_inventory\u001b[49m(customer_orders, inventory)\n\u001b[32m 6\u001b[39m order_stats = calculate_order_statistics(customer_orders, products)\n\u001b[32m 8\u001b[39m print_order_statistics(order_stats)\n", + "\u001b[31mNameError\u001b[39m: name 'update_inventory' is not defined" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "7c8cdd69-8854-42c2-806d-bac027fd7167", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for apple: 30\n", + "Enter quantity for banana: 24\n", + "Enter quantity for orange: 22\n", + "Enter quantity for grape: 21\n", + "Enter a product to order (or 'done' to finish): grapes\n", + "Enter a product to order (or 'done' to finish): orange\n", + "Enter a product to order (or 'done' to finish): done\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, grapes is not available or out of stock.\n", + "\n", + "Order Statistics:\n", + "Total unique products ordered: 2\n", + "Percentage of available products ordered: 50.00%\n", + "\n", + "Updated Inventory:\n", + "apple: 30\n", + "banana: 24\n", + "orange: 21\n", + "grape: 21\n" + ] + } + ], + "source": [ + "# ----------------------------\n", + "# Function Definitions\n", + "# ----------------------------\n", + "\n", + "# Initialize inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "# Get customer orders\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter a product to order (or 'done' to finish): \").strip()\n", + " if order.lower() == \"done\":\n", + " break\n", + " customer_orders.add(order)\n", + " return customer_orders\n", + "\n", + "# Update inventory\n", + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory and inventory[order] > 0:\n", + " inventory[order] -= 1\n", + " else:\n", + " print(f\"Sorry, {order} is not available or out of stock.\")\n", + " return inventory\n", + "\n", + "# Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique = (len(customer_orders) / len(products)) * 100\n", + " return total_products_ordered, percentage_unique\n", + "\n", + "# Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total unique products ordered: {total}\")\n", + " print(f\"Percentage of available products ordered: {percentage:.2f}%\")\n", + "\n", + "# Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "# ----------------------------\n", + "# Main Program\n", + "# ----------------------------\n", + "\n", + "products = [\"apple\", \"banana\", \"orange\", \"grape\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_stats)\n", + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "2c086c63-c21b-47bb-9877-2defb194e0ee", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for apple: 2\n", + "Enter quantity for banana: 4\n", + "Enter quantity for orange: 6\n", + "Enter quantity for grape: 8\n", + "Enter a product to order (or 'done' to finish): done\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total unique products ordered: 0\n", + "Percentage of available products ordered: 0.00%\n" + ] + } + ], + "source": [ + "products = [\"apple\", \"banana\", \"orange\", \"grape\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_stats)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "59dec4a3-8278-47f8-b7a5-710c7bdc4ebd", + "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.13.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..5c79c70 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,287 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "a0e25348-319a-4b12-95fe-5f609482646f", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Initialize inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "be4e90b8-e93d-4b6b-941c-4a1fee50e47b", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Get customer orders\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter a product to order (or 'done' to finish): \").strip()\n", + " if order.lower() == \"done\":\n", + " break\n", + " customer_orders.add(order)\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "053743fe-45b6-4ec7-b001-6c4be5cee5a3", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory and inventory[order] > 0:\n", + " inventory[order] -= 1\n", + " else:\n", + " print(f\"Sorry, {order} is not available or out of stock.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "8bc7c17b-91db-4ab6-bf6e-df64e6aee751", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique = (len(customer_orders) / len(products)) * 100\n", + " return total_products_ordered, percentage_unique" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "e8570722-9eae-494c-bcc1-2f9936ef4b10", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 5: Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total unique products ordered: {total}\")\n", + " print(f\"Percentage of available products ordered: {percentage:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "bb510941-b885-4e2c-bcb9-bbcd7e658a17", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 6: Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "46e4aca3-d5a7-4e28-b546-585f6191f017", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for apple: 20\n", + "Enter quantity for banana: 12\n", + "Enter quantity for orange: 24\n", + "Enter quantity for grape: 30\n", + "Enter a product to order (or 'done' to finish): bnana\n", + "Enter a product to order (or 'done' to finish): orange\n", + "Enter a product to order (or 'done' to finish): papaya\n", + "Enter a product to order (or 'done' to finish): grape\n", + "Enter a product to order (or 'done' to finish): pple\n", + "Enter a product to order (or 'done' to finish): done\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'update_inventory' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[8]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[32m 3\u001b[39m inventory = initialize_inventory(products)\n\u001b[32m 4\u001b[39m customer_orders = get_customer_orders()\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m inventory = \u001b[43mupdate_inventory\u001b[49m(customer_orders, inventory)\n\u001b[32m 6\u001b[39m order_stats = calculate_order_statistics(customer_orders, products)\n\u001b[32m 8\u001b[39m print_order_statistics(order_stats)\n", + "\u001b[31mNameError\u001b[39m: name 'update_inventory' is not defined" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "7c8cdd69-8854-42c2-806d-bac027fd7167", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for apple: 30\n", + "Enter quantity for banana: 24\n", + "Enter quantity for orange: 22\n", + "Enter quantity for grape: 21\n", + "Enter a product to order (or 'done' to finish): grapes\n", + "Enter a product to order (or 'done' to finish): orange\n", + "Enter a product to order (or 'done' to finish): done\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, grapes is not available or out of stock.\n", + "\n", + "Order Statistics:\n", + "Total unique products ordered: 2\n", + "Percentage of available products ordered: 50.00%\n", + "\n", + "Updated Inventory:\n", + "apple: 30\n", + "banana: 24\n", + "orange: 21\n", + "grape: 21\n" + ] + } + ], + "source": [ + "# ----------------------------\n", + "# Function Definitions\n", + "# ----------------------------\n", + "\n", + "# Initialize inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "# Get customer orders\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter a product to order (or 'done' to finish): \").strip()\n", + " if order.lower() == \"done\":\n", + " break\n", + " customer_orders.add(order)\n", + " return customer_orders\n", + "\n", + "# Update inventory\n", + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory and inventory[order] > 0:\n", + " inventory[order] -= 1\n", + " else:\n", + " print(f\"Sorry, {order} is not available or out of stock.\")\n", + " return inventory\n", + "\n", + "# Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique = (len(customer_orders) / len(products)) * 100\n", + " return total_products_ordered, percentage_unique\n", + "\n", + "# Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total unique products ordered: {total}\")\n", + " print(f\"Percentage of available products ordered: {percentage:.2f}%\")\n", + "\n", + "# Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "# ----------------------------\n", + "# Main Program\n", + "# ----------------------------\n", + "\n", + "products = [\"apple\", \"banana\", \"orange\", \"grape\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_stats)\n", + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "2c086c63-c21b-47bb-9877-2defb194e0ee", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for apple: 2\n", + "Enter quantity for banana: 4\n", + "Enter quantity for orange: 6\n", + "Enter quantity for grape: 8\n", + "Enter a product to order (or 'done' to finish): done\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total unique products ordered: 0\n", + "Percentage of available products ordered: 0.00%\n" + ] + } + ], + "source": [ + "products = [\"apple\", \"banana\", \"orange\", \"grape\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_stats)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "59dec4a3-8278-47f8-b7a5-710c7bdc4ebd", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +342,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.6" } }, "nbformat": 4,