From b2ffa29a47e3d7045c6b90797c2f6e0013783504 Mon Sep 17 00:00:00 2001 From: Irma Fernandez Date: Wed, 3 Sep 2025 18:15:02 +0200 Subject: [PATCH] Funtions Lab done --- .../lab-python-functions-checkpoint.ipynb | 267 ++++++++++++++++++ lab-python-functions.ipynb | 200 ++++++++++++- 2 files changed, 466 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..ef7da6a --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,267 @@ +{ + "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": 1, + "id": "e8df1555-fa94-4d84-9078-e166cb079869", + "metadata": {}, + "outputs": [], + "source": [ + "# Product list from previous labs\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# 1 Initialize_inventory(products)\n", + "\n", + "def initialize_inventory(products):\n", + " \"\"\"\n", + " Ask for quantities once per product and build the inventory dict.\n", + " Returns: dict{product: quantity}\n", + " \"\"\"\n", + " inventory = {}\n", + " for p in products:\n", + " qty= int(input(f\"Enter the quantity for {p}: \"))\n", + " inventory[p] = qty\n", + " return inventory\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "16d28735-d4df-4da0-93a6-4f6c87b03325", + "metadata": {}, + "outputs": [], + "source": [ + "# 2 Define get_customer_orders(products)\n", + "\n", + "def get_customer_orders(products):\n", + " \"\"\"\n", + " Ask the user for product names in a lopp.\n", + " Returns: set of unique ordered products. \n", + " \"\"\"\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter the name of a product to order: \")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(f\"{order} is not available.\")\n", + " more = input(\"Do you want to add another product? (yes/no): \")\n", + " if more != \"yes\":\n", + " break\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "51d74c53-e1c8-4051-ad0a-71ae396ec0d6", + "metadata": {}, + "outputs": [], + "source": [ + "# 3 Define update_inventory(customer_orders,inventory)\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Substract 1 from each ordered product that exist in the inventory.\n", + " Returne the updated inventory dict.\n", + " \"\"\"\n", + " for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1\n", + " return inventory\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "1e64de76-f594-4045-bee0-a83202308a3c", + "metadata": {}, + "outputs": [], + "source": [ + "#4 Define calculate_order_statistics\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"\n", + " Compute total products ordered and percentage of catalog.\n", + " Returns: (total, percentage)\n", + " \"\"\"\n", + " total = len(customer_orders)\n", + " percentage = (total / len(products)) * 100\n", + " return (total, percentage)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a93885e5-e792-476c-bbb1-eea156622b19", + "metadata": {}, + "outputs": [], + "source": [ + "#5 Define print_order_statistics\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"\n", + " Print the order statistics tuple.\n", + " \"\"\"\n", + " total, percentage = order_statistics\n", + " print(\"Order Statistics\")\n", + " print(\"Total Products Ordered:\", total)\n", + " print(\"Percentage of Products Ordered:\", percentage)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "066d9ded-13cc-4745-bfd5-2c5165a5c44d", + "metadata": {}, + "outputs": [], + "source": [ + "#6 Define print _updated_inventory\n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"\n", + " Print inventory, one line per item format. \n", + " \"\"\"\n", + " print(\"Updated inventory:\")\n", + " for product in inventory:\n", + " print(product, \":\", inventory[product])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "526cb404-5c10-4a62-84f5-295a8fe8490f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 45\n", + "Enter the quantity for mug: 85\n", + "Enter the quantity for hat: 65\n", + "Enter the quantity for book: 25\n", + "Enter the quantity for keychain: 30\n", + "Enter the name of a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 20.0\n", + "Updated inventory:\n", + "t-shirt : 45\n", + "mug : 85\n", + "hat : 64\n", + "book : 25\n", + "keychain : 30\n" + ] + } + ], + "source": [ + "#7 Run the program in sequence\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "#1 Initialize inventory\n", + "inventory = initialize_inventory(products)\n", + "\n", + "#2 Get customer orders\n", + "customer_orders = get_customer_orders(products)\n", + "\n", + "#3 Update Inventory\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "#4 Calculate order statistics\n", + "stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "#5 Print order statistic\n", + "print_order_statistics(stats)\n", + "\n", + "#6 Print updated inventory\n", + "print_updated_inventory(inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc3bcec4-4f2f-4762-af81-a1481e733c8f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..ef7da6a 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,204 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "e8df1555-fa94-4d84-9078-e166cb079869", + "metadata": {}, + "outputs": [], + "source": [ + "# Product list from previous labs\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# 1 Initialize_inventory(products)\n", + "\n", + "def initialize_inventory(products):\n", + " \"\"\"\n", + " Ask for quantities once per product and build the inventory dict.\n", + " Returns: dict{product: quantity}\n", + " \"\"\"\n", + " inventory = {}\n", + " for p in products:\n", + " qty= int(input(f\"Enter the quantity for {p}: \"))\n", + " inventory[p] = qty\n", + " return inventory\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "16d28735-d4df-4da0-93a6-4f6c87b03325", + "metadata": {}, + "outputs": [], + "source": [ + "# 2 Define get_customer_orders(products)\n", + "\n", + "def get_customer_orders(products):\n", + " \"\"\"\n", + " Ask the user for product names in a lopp.\n", + " Returns: set of unique ordered products. \n", + " \"\"\"\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter the name of a product to order: \")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(f\"{order} is not available.\")\n", + " more = input(\"Do you want to add another product? (yes/no): \")\n", + " if more != \"yes\":\n", + " break\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "51d74c53-e1c8-4051-ad0a-71ae396ec0d6", + "metadata": {}, + "outputs": [], + "source": [ + "# 3 Define update_inventory(customer_orders,inventory)\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Substract 1 from each ordered product that exist in the inventory.\n", + " Returne the updated inventory dict.\n", + " \"\"\"\n", + " for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1\n", + " return inventory\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "1e64de76-f594-4045-bee0-a83202308a3c", + "metadata": {}, + "outputs": [], + "source": [ + "#4 Define calculate_order_statistics\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"\n", + " Compute total products ordered and percentage of catalog.\n", + " Returns: (total, percentage)\n", + " \"\"\"\n", + " total = len(customer_orders)\n", + " percentage = (total / len(products)) * 100\n", + " return (total, percentage)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a93885e5-e792-476c-bbb1-eea156622b19", + "metadata": {}, + "outputs": [], + "source": [ + "#5 Define print_order_statistics\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"\n", + " Print the order statistics tuple.\n", + " \"\"\"\n", + " total, percentage = order_statistics\n", + " print(\"Order Statistics\")\n", + " print(\"Total Products Ordered:\", total)\n", + " print(\"Percentage of Products Ordered:\", percentage)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "066d9ded-13cc-4745-bfd5-2c5165a5c44d", + "metadata": {}, + "outputs": [], + "source": [ + "#6 Define print _updated_inventory\n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"\n", + " Print inventory, one line per item format. \n", + " \"\"\"\n", + " print(\"Updated inventory:\")\n", + " for product in inventory:\n", + " print(product, \":\", inventory[product])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "526cb404-5c10-4a62-84f5-295a8fe8490f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 45\n", + "Enter the quantity for mug: 85\n", + "Enter the quantity for hat: 65\n", + "Enter the quantity for book: 25\n", + "Enter the quantity for keychain: 30\n", + "Enter the name of a product to order: hat\n", + "Do you want to add another product? (yes/no): yes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 20.0\n", + "Updated inventory:\n", + "t-shirt : 45\n", + "mug : 85\n", + "hat : 64\n", + "book : 25\n", + "keychain : 30\n" + ] + } + ], + "source": [ + "#7 Run the program in sequence\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "#1 Initialize inventory\n", + "inventory = initialize_inventory(products)\n", + "\n", + "#2 Get customer orders\n", + "customer_orders = get_customer_orders(products)\n", + "\n", + "#3 Update Inventory\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "#4 Calculate order statistics\n", + "stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "#5 Print order statistic\n", + "print_order_statistics(stats)\n", + "\n", + "#6 Print updated inventory\n", + "print_updated_inventory(inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc3bcec4-4f2f-4762-af81-a1481e733c8f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +259,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,