diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..9facd6d 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -1,13 +1,5 @@ { "cells": [ - { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", - "metadata": {}, - "source": [ - "# Lab | Functions" - ] - }, { "cell_type": "markdown", "id": "0c581062-8967-4d93-b06e-62833222f930", @@ -43,11 +35,299 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "c23485dc", + "metadata": {}, + "outputs": [], + "source": [ + "products =[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "8084bcc2", + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Define a function named initialize_inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " break\n", + " else:\n", + " print(\"Please enter a non-negative integer.\")\n", + " except ValueError:\n", + " print(\"Please enter a valid integer.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1ee70a97", + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Define a function named get_customer_orders\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " \n", + " while True:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " customer_orders.add(product)\n", + " \n", + " while True:\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another in ['yes', 'y']:\n", + " break\n", + " elif another in ['no', 'n']:\n", + " return customer_orders\n", + " else:\n", + " print(\"Please enter 'yes' or 'no'.\")\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "91d1226e", + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Define a function named update_inventory\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Update inventory by decreasing quantities based on customer orders.\n", + " \n", + " Parameters:\n", + " customer_orders (set): Set of ordered product names\n", + " inventory (dict): Dictionary with products as keys and quantities as values\n", + " \"\"\"\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Warning: {product} is not available or out of stock.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "19fabc9f", + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Define a function named calculate_order_statistics\n", + "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", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "386a4279", + "metadata": {}, + "outputs": [], + "source": [ + "# 5. Define a function named print_order_statistics\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage_ordered:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "f92c622c", + "metadata": {}, + "outputs": [], + "source": [ + "# 6. Define a function named 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": 12, + "id": "aba969a5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Welcome to the Customer Order Management System!\n", + "Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "--- Initializing Inventory ---\n", + "\n", + "--- Getting Customer Orders ---\n", + "\n", + "--- Getting Customer Orders ---\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Unique Products Ordered: 20.00%\n", + "\n", + "--- Updating Inventory ---\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 2\n", + "mug: 1\n", + "hat: 2\n", + "book: 2\n", + "keychain: 2\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Unique Products Ordered: 20.00%\n", + "\n", + "--- Updating Inventory ---\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 2\n", + "mug: 1\n", + "hat: 2\n", + "book: 2\n", + "keychain: 2\n" + ] + } + ], + "source": [ + "# 7. Main execution - Call the functions in the appropriate sequence\n", + "def main():\n", + " # Define the list of products\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " \n", + " print(\"Welcome to the Customer Order Management System!\")\n", + " print(f\"Available products: {products}\")\n", + " \n", + " # Step 1: Initialize inventory\n", + " print(\"\\n--- Initializing Inventory ---\")\n", + " inventory = initialize_inventory(products)\n", + " \n", + " # Step 2: Get customer orders\n", + " print(\"\\n--- Getting Customer Orders ---\")\n", + " customer_orders = get_customer_orders()\n", + " \n", + " # Step 3: Calculate order statistics (before updating inventory)\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " \n", + " # Step 4: Print order statistics\n", + " print_order_statistics(order_statistics)\n", + " \n", + " # Step 5: Update inventory\n", + " print(\"\\n--- Updating Inventory ---\")\n", + " update_inventory(customer_orders, inventory)\n", + " \n", + " # Step 6: Print updated inventory\n", + " print_updated_inventory(inventory)\n", + "\n", + "# Run the main function\n", + "if __name__ == \"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "e62beeef", + "metadata": {}, + "outputs": [], + "source": [ + "# Optional: Test individual functions\n", + "def test_functions():\n", + " \"\"\"\n", + " Test individual functions with sample data to ensure they work correctly.\n", + " \"\"\"\n", + " print(\"=== Testing Individual Functions ===\")\n", + " \n", + " # Test data\n", + " test_products = [\"t-shirt\", \"mug\", \"hat\"]\n", + " test_inventory = {\"t-shirt\": 5, \"mug\": 3, \"hat\": 2}\n", + " test_orders = {\"t-shirt\", \"mug\"} # Set of ordered products\n", + " \n", + " print(f\"Test products: {test_products}\")\n", + " print(f\"Test inventory: {test_inventory}\")\n", + " print(f\"Test orders: {test_orders}\")\n", + " \n", + " # Test calculate_order_statistics\n", + " stats = calculate_order_statistics(test_orders, test_products)\n", + " print(f\"\\nOrder statistics: {stats}\")\n", + " \n", + " # Test print_order_statistics\n", + " print_order_statistics(stats)\n", + " \n", + " # Test update_inventory\n", + " print(f\"\\nInventory before update: {test_inventory}\")\n", + " update_inventory(test_orders, test_inventory)\n", + " print(f\"Inventory after update: {test_inventory}\")\n", + " \n", + " # Test print_updated_inventory\n", + " print_updated_inventory(test_inventory)\n", + "\n", + "# Uncomment the line below to run tests\n", + "# test_functions()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "fa294113", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "=== Testing Individual Functions ===\n", + "Test products: ['t-shirt', 'mug', 'hat']\n", + "Test inventory: {'t-shirt': 5, 'mug': 3, 'hat': 2}\n", + "Test orders: {'mug', 't-shirt'}\n", + "\n", + "Order statistics: (2, 66.66666666666666)\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 66.67%\n", + "\n", + "Inventory before update: {'t-shirt': 5, 'mug': 3, 'hat': 2}\n", + "Inventory after update: {'t-shirt': 4, 'mug': 2, 'hat': 2}\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 4\n", + "mug: 2\n", + "hat: 2\n" + ] + } + ], + "source": [ + "# Run the test function to verify all functions work correctly\n", + "test_functions()" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +341,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,