From c1e3a9932ce69318ab01ec4257f340b94f02809b Mon Sep 17 00:00:00 2001 From: Francisca Date: Wed, 18 Feb 2026 15:28:09 +0000 Subject: [PATCH] Resolution lab functions --- .../lab-python-functions-checkpoint.ipynb | 69 ++++ lab-python-functions.ipynb | 304 +++++++++++++++++- 2 files changed, 372 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb 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..fc527bd 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,308 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "29555728-8f38-4346-afa8-8c5a2d66dde7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 4\n", + "Enter the quantity for mug: 5\n", + "Enter the quantity for hat: 6\n", + "Enter the quantity for book: 5\n", + "Enter the quantity for keychain: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory: {'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 5, 'keychain': 4}\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "print(\"Inventory:\", inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "0c662a84-c769-439b-b807-c8ec702d602f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: mug\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer orders: {'mug'}\n" + ] + } + ], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " product = input(\"Enter a product to order: \")\n", + " customer_orders.add(product)\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if another != \"yes\":\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(\"Customer orders:\", orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "220eb989-b418-4815-92f4-fe521c78a7e1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: {'t-shirt': 4, 'mug': 4, 'hat': 6, 'book': 5, 'keychain': 4}\n" + ] + } + ], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Sorry, {product} is out of stock or not available.\")\n", + "\n", + "\n", + "inventory = {'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 5, 'keychain': 4}\n", + "customer_orders = {\"mug\"}\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "print(\"Updated inventory:\", inventory)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "a45e36ab-fb37-4ebc-aff8-92e46997cc93", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 20.0 %\n" + ] + } + ], + "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", + " return total_products_ordered, percentage_ordered \n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = {\"mug\"}\n", + "\n", + "total, percentage = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print(\"Total Products Ordered:\", total)\n", + "print(\"Percentage of Products Ordered:\", percentage, \"%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "f24217ce-c138-4f1e-8af8-f8894c0de14d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 20.0)\n" + ] + } + ], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + "\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total_products_ordered)\n", + " print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")\n", + " return order_statistics\n", + " \n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = {\"mug\"}\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(\"Order Statistics:\", order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "5ced9cab-a8fe-40a1-ac5c-7756b6667b6b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 4\n", + "mug: 4\n", + "hat: 6\n", + "book: 5\n", + "keychain: 4\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "Inventory = {'t-shirt': 4, 'mug': 4, 'hat': 6, 'book': 5, 'keychain': 4}\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "ce5f370a-36f9-41af-b91f-bbeee9252d65", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 5\n", + "Enter the quantity for mug: 5\n", + "Enter the quantity for hat: 5\n", + "Enter the quantity for book: 5\n", + "Enter the quantity for keychain: 5\n", + "Enter a product to order: mug\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics: (1, 20.0)\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 5\n", + "book: 5\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " product = input(\"Enter a product to order: \")\n", + " customer_orders.add(product)\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").lower()\n", + " if another != \"yes\":\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Sorry, {product} is out of stock or not available.\")\n", + "\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 \n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + "\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total_products_ordered)\n", + " print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")\n", + " return order_statistics\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "\n", + "total, percentage = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print(\"Order Statistics:\", order_statistics)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7f7326fa-b0b1-4f45-8e71-cb5681f6d921", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +363,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,