diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..44d337b --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,69 @@ +{ + "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" + ] + } + ], + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..952766b 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,271 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7f033c69-8af8-4001-9f74-527dfac0fa28", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " print(\"šŸ“¦ Initialize Inventory\")\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " qty = int(input(f\"Enter quantity for {product.title()}: \"))\n", + " if qty >= 0:\n", + " inventory[product] = qty\n", + " break\n", + " else:\n", + " print(\"Please enter a non-negative number.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a number.\")\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "eba4b58a-2fd4-448d-84ee-4a8b3fc7d3fc", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " print(\"\\nšŸ›’ Enter Customer Orders\")\n", + " \n", + " while True:\n", + " product = input(\"Enter a product to order: \").lower().strip()\n", + " customer_orders.add(product)\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != \"yes\":\n", + " break\n", + " \n", + " return customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "26a38dc7-ab7b-4fa6-9522-3cd007999a12", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " print(\"\\nšŸ”„ Updating Inventory\")\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " print(f\"{product.title()} inventory updated to {inventory[product]}\")\n", + " elif product in inventory:\n", + " print(f\"{product.title()} is out of stock!\")\n", + " else:\n", + " print(f\"{product.title()} not found in inventory.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "547fadd5-fe27-4679-aa59-ad0b70ba0d2e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "šŸ“¦ Initialize Inventory\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for Laptop: 5\n", + "Enter quantity for Phone: 9\n", + "Enter quantity for Headphones: 8\n", + "Enter quantity for Monitor: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "šŸ›’ Enter Customer Orders\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: phone\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: laptop\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: monitor\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "šŸ”„ Updating Inventory\n", + "Laptop inventory updated to 4\n", + "Monitor inventory updated to 9\n", + "Phone inventory updated to 8\n", + "\n", + "āœ… Final inventory state: {'laptop': 4, 'phone': 8, 'headphones': 8, 'monitor': 9}\n", + "āœ… Products ordered: {'laptop', 'monitor', 'phone'}\n" + ] + } + ], + "source": [ + "# Sample product list to use in initialization\n", + "products = [\"laptop\", \"phone\", \"headphones\", \"monitor\"]\n", + "\n", + "# Step 1: Initialize inventory by calling the function\n", + "inventory = initialize_inventory(products)\n", + "\n", + "# Step 2: Get customer orders\n", + "customer_orders = get_customer_orders()\n", + "\n", + "# Step 3: Update the inventory based on orders\n", + "update_inventory(customer_orders, inventory)\n", + "\n", + "# Just to check what's in the data so far:\n", + "print(\"\\nāœ… Final inventory state:\", inventory)\n", + "print(\"āœ… Products ordered:\", customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "71f255d5-8d84-4b0c-a403-034d3ff4f169", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_ordered = len(customer_orders)\n", + " unique_percentage = (total_ordered / len(products)) * 100\n", + " return total_ordered, unique_percentage\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "681ff86e-85e5-48aa-a274-2fa89dda03b6", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_stats):\n", + " total_ordered, unique_percentage = order_stats\n", + " print(\"\\nšŸ“Š Order Statistics\")\n", + " print(f\"Total unique products ordered: {total_ordered}\")\n", + " print(f\"Percentage of catalog ordered: {unique_percentage:.2f}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "e0ce0073-677f-491d-b6d9-119ff6942b85", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"\\nšŸ“¦ Final Inventory\")\n", + " for product, qty in inventory.items():\n", + " print(f\"{product.title()}: {qty} in stock\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "a1b5165c-1151-4e1b-bae4-c1aa6cf50cff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "šŸ“¦ Initialize Inventory\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for Laptop: 5\n", + "Enter quantity for Phone: 9\n", + "Enter quantity for Headphones: 8\n", + "Enter quantity for Monitor: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "šŸ›’ Enter Customer Orders\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: laptop\n", + "Do you want to add another product? (yes/no): phone\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "šŸ”„ Updating Inventory\n", + "Laptop inventory updated to 4\n", + "\n", + "šŸ“Š Order Statistics\n", + "Total unique products ordered: 1\n", + "Percentage of catalog ordered: 25.00%\n", + "\n", + "šŸ“¦ Final Inventory\n", + "Laptop: 4 in stock\n", + "Phone: 9 in stock\n", + "Headphones: 8 in stock\n", + "Monitor: 10 in stock\n" + ] + } + ], + "source": [ + "##### Product catalog\n", + "products = [\"laptop\", \"phone\", \"headphones\", \"monitor\"]\n", + "\n", + "# Run full order process\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "update_inventory(customer_orders, inventory)\n", + "\n", + "# Generate and print statistics\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_stats)\n", + "\n", + "# Show final inventory\n", + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf8a4133-f61d-45ed-95f5-2d7501dee11d", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +326,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.2" } }, "nbformat": 4,