diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..6b96d4f 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -1,55 +1,186 @@ { "cells": [ { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "cell_type": "code", + "execution_count": 2, + "id": "a90b5f52-6443-4a0f-87eb-a49b2b10cf37", "metadata": {}, + "outputs": [], "source": [ - "# Lab | Functions" + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " print(\"\\n--- Initialize Inventory ---\")\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory" ] }, { - "cell_type": "markdown", - "id": "0c581062-8967-4d93-b06e-62833222f930", - "metadata": { - "tags": [] - }, + "cell_type": "code", + "execution_count": 11, + "id": "ea8f892b-d191-4310-bc62-49d8066a66f4", + "metadata": {}, + "outputs": [], "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", + "def get_customer_order():\n", + " customer_orders = set()\n", + " print(\"\\n---Customer Orders---\")\n", + " while True:\n", + " order = input(\"Enter a product to order: \").lower()\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Invalid product. Please try again.\")\n", + " continue\n", + " another_product = input(\"Add another product? (yes/no): \").lower()\n", + " if another_product == \"no\":\n", + " break\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "1aa5cbaa-dc38-46cb-a167-2f8a89e0d9bc", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "b19a3610-efd8-41eb-aea1-9f11c341e2a1", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\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", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "e3497a6a-4455-46dc-b95f-0210b4a65efb", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"\\nOrder 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": 15, + "id": "9797931f-68e7-4c5c-b5c0-cc19fe5aee03", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", "\n", - "\n" + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "7f36b785-8ea8-4069-8190-67e5b05adb0d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Initialize Inventory ---\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt: 10\n", + "Enter quantity for mug: 20\n", + "Enter quantity for hat: 40\n", + "Enter quantity for book: 18\n", + "Enter quantity for keychain: 88\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "---Customer Orders---\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: mug\n", + "Add another product? (yes/no): yes\n", + "Enter a product to order: book\n", + "Add another product? (yes/no): yes\n", + "Enter a product to order: keychain\n", + "Add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 19\n", + "hat: 40\n", + "book: 17\n", + "keychain: 87\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_order()\n", + "update_inventory(customer_orders, inventory)\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_stats)\n", + "print_updated_inventory(inventory)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ccdcc59-4bd4-4bb6-9743-a47214f3e8e4", + "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 +192,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,