diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..1688527 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,349 @@ +{ + "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": 4, + "id": "fde839be-1b1f-44b6-afb1-122df6c36549", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = input(f\"Enter quantity for {product}: \")\n", + " inventory[product] = int(quantity)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1aca14dd-5ff6-42fc-b884-23243ce92fe0", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for tshirt: 11\n", + "Enter quantity for mug: 33\n", + "Enter quantity for hat: 5\n", + "Enter quantity for book: 7\n", + "Enter quantity for keychain: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 11, 'mug': 33, 'hat': 5, 'book': 7, 'keychain': 9}\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "92e5e319-b383-4ef6-b729-6cf37d5f8a76", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " another = \"yes\"\n", + " while another == \"yes\":\n", + " order = input(\"Enter a product to order: \").lower()\n", + " customer_orders.add(order)\n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " return customer_orders " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "913af362-e24f-4eb0-8a97-46149cb5f3ce", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: keychain\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'keychain', 'hat'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "18520ce5-6035-452a-a1f3-855b83080a28", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\n", + " return inventory " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "bd086d65-3b33-4efe-9704-b06f0771d2cd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 11, 'mug': 33, 'hat': 4, 'book': 6, 'keychain': 8}\n" + ] + } + ], + "source": [ + "inventory = update_inventory(customer_orders, inventory)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "06276879-30e7-4e68-b88b-beda6dff040e", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (len(customer_orders) / len(products)) *100\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "fcab108a-5a65-4c54-97a7-1a7fa05201c2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "8eb2eaf5-6d61-468b-9065-20c57a79907d", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of Products Ordered: {order_statistics[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "aa59f931-5f60-4fbe-873c-d0391bc0d962", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "9f2c68e4-f94f-44b9-8181-e6afb4f44005", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "368154b2-56d2-4654-b424-f5225734acce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tshirt: 11\n", + "mug: 33\n", + "hat: 4\n", + "book: 6\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "6dd73881-06e0-4aad-8b55-55c9ae06b5a8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for tshirt: 22\n", + "Enter quantity for mug: 12\n", + "Enter quantity for hat: 5\n", + "Enter quantity for book: 7\n", + "Enter quantity for keychain: 10\n", + "Enter a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.0%\n", + "tshirt: 22\n", + "mug: 12\n", + "hat: 4\n", + "book: 6\n", + "keychain: 10\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "724b4d2c-f8c7-446a-b31c-b248b8fbcd92", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..1688527 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,293 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "fde839be-1b1f-44b6-afb1-122df6c36549", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = input(f\"Enter quantity for {product}: \")\n", + " inventory[product] = int(quantity)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1aca14dd-5ff6-42fc-b884-23243ce92fe0", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for tshirt: 11\n", + "Enter quantity for mug: 33\n", + "Enter quantity for hat: 5\n", + "Enter quantity for book: 7\n", + "Enter quantity for keychain: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 11, 'mug': 33, 'hat': 5, 'book': 7, 'keychain': 9}\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "92e5e319-b383-4ef6-b729-6cf37d5f8a76", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " another = \"yes\"\n", + " while another == \"yes\":\n", + " order = input(\"Enter a product to order: \").lower()\n", + " customer_orders.add(order)\n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " return customer_orders " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "913af362-e24f-4eb0-8a97-46149cb5f3ce", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: keychain\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'keychain', 'hat'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "18520ce5-6035-452a-a1f3-855b83080a28", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\n", + " return inventory " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "bd086d65-3b33-4efe-9704-b06f0771d2cd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'tshirt': 11, 'mug': 33, 'hat': 4, 'book': 6, 'keychain': 8}\n" + ] + } + ], + "source": [ + "inventory = update_inventory(customer_orders, inventory)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "06276879-30e7-4e68-b88b-beda6dff040e", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (len(customer_orders) / len(products)) *100\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "fcab108a-5a65-4c54-97a7-1a7fa05201c2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "8eb2eaf5-6d61-468b-9065-20c57a79907d", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of Products Ordered: {order_statistics[1]}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "aa59f931-5f60-4fbe-873c-d0391bc0d962", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n" + ] + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "9f2c68e4-f94f-44b9-8181-e6afb4f44005", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "368154b2-56d2-4654-b424-f5225734acce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tshirt: 11\n", + "mug: 33\n", + "hat: 4\n", + "book: 6\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "6dd73881-06e0-4aad-8b55-55c9ae06b5a8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for tshirt: 22\n", + "Enter quantity for mug: 12\n", + "Enter quantity for hat: 5\n", + "Enter quantity for book: 7\n", + "Enter quantity for keychain: 10\n", + "Enter a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.0%\n", + "tshirt: 22\n", + "mug: 12\n", + "hat: 4\n", + "book: 6\n", + "keychain: 10\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "724b4d2c-f8c7-446a-b31c-b248b8fbcd92", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -61,7 +341,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,