From 65c1eeabfffb034ff433c49fd86ee40da08ce635 Mon Sep 17 00:00:00 2001 From: mariamnez Date: Mon, 28 Jul 2025 11:04:03 +0200 Subject: [PATCH 1/2] Solved lab --- lab-python-functions-completed.ipynb | 146 +++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 lab-python-functions-completed.ipynb diff --git a/lab-python-functions-completed.ipynb b/lab-python-functions-completed.ipynb new file mode 100644 index 0000000..28f649f --- /dev/null +++ b/lab-python-functions-completed.ipynb @@ -0,0 +1,146 @@ +{ + "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": null, + "id": "1ca32287", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# Define a function to create an order\n", + "def create_order(customer_name, item, quantity):\n", + " return {\n", + " 'customer': customer_name,\n", + " 'item': item,\n", + " 'quantity': quantity,\n", + " 'status': 'pending'\n", + " }\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b901fa7c", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# Define a function to process an order\n", + "def process_order(order):\n", + " order['status'] = 'processed'\n", + " return order\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c635d29", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# Define a function to display order summary\n", + "def display_order(order):\n", + " print(f\"Order Summary:\")\n", + " print(f\"Customer: {order['customer']}\")\n", + " print(f\"Item: {order['item']}\")\n", + " print(f\"Quantity: {order['quantity']}\")\n", + " print(f\"Status: {order['status']}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bbf0a042", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# Define a function to handle a full order process\n", + "def handle_order(customer_name, item, quantity):\n", + " order = create_order(customer_name, item, quantity)\n", + " processed_order = process_order(order)\n", + " display_order(processed_order)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ee0a034f", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# Sample usage\n", + "handle_order(\"Alice\", \"Laptop\", 2)\n", + "handle_order(\"Bob\", \"Smartphone\", 1)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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 +} From d4f11720df859b62e82ef91a0255a9d1fb33d284 Mon Sep 17 00:00:00 2001 From: mariamnez Date: Mon, 28 Jul 2025 11:23:31 +0200 Subject: [PATCH 2/2] Solved lab --- ...ipynb => lab-python-functions-solved.ipynb | 104 ++++++++++++------ 1 file changed, 72 insertions(+), 32 deletions(-) rename lab-python-functions-completed.ipynb => lab-python-functions-solved.ipynb (55%) diff --git a/lab-python-functions-completed.ipynb b/lab-python-functions-solved.ipynb similarity index 55% rename from lab-python-functions-completed.ipynb rename to lab-python-functions-solved.ipynb index 28f649f..b57d2ed 100644 --- a/lab-python-functions-completed.ipynb +++ b/lab-python-functions-solved.ipynb @@ -47,78 +47,118 @@ { "cell_type": "code", "execution_count": null, - "id": "1ca32287", + "id": "f4ec232e", "metadata": {}, "outputs": [], "source": [ "\n", - "# Define a function to create an order\n", - "def create_order(customer_name, item, quantity):\n", - " return {\n", - " 'customer': customer_name,\n", - " 'item': item,\n", - " 'quantity': quantity,\n", - " 'status': 'pending'\n", - " }\n" + "# 1. Define initialize_inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter initial quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "b901fa7c", + "id": "f4eb3d05", "metadata": {}, "outputs": [], "source": [ "\n", - "# Define a function to process an order\n", - "def process_order(order):\n", - " order['status'] = 'processed'\n", - " return order\n" + "# 2. Define get_customer_orders\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter a product name to order (or type 'done' to finish): \").strip()\n", + " if order.lower() == 'done':\n", + " break\n", + " customer_orders.add(order)\n", + " return customer_orders\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "0c635d29", + "id": "76f84661", "metadata": {}, "outputs": [], "source": [ "\n", - "# Define a function to display order summary\n", - "def display_order(order):\n", - " print(f\"Order Summary:\")\n", - " print(f\"Customer: {order['customer']}\")\n", - " print(f\"Item: {order['item']}\")\n", - " print(f\"Quantity: {order['quantity']}\")\n", - " print(f\"Status: {order['status']}\")\n" + "# 3. Define update_inventory\n", + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory and inventory[order] > 0:\n", + " inventory[order] -= 1\n", + " else:\n", + " print(f\"Product '{order}' is out of stock or not found.\")\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "bbf0a042", + "id": "64c2c274", "metadata": {}, "outputs": [], "source": [ "\n", - "# Define a function to handle a full order process\n", - "def handle_order(customer_name, item, quantity):\n", - " order = create_order(customer_name, item, quantity)\n", - " processed_order = process_order(order)\n", - " display_order(processed_order)\n" + "# 4. Define calculate_order_statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " unique_products_ordered = len([p for p in customer_orders if p in products])\n", + " percentage_unique = (unique_products_ordered / len(products)) * 100 if products else 0\n", + " return total_products_ordered, percentage_unique\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "ee0a034f", + "id": "d14de91e", "metadata": {}, "outputs": [], "source": [ "\n", - "# Sample usage\n", - "handle_order(\"Alice\", \"Laptop\", 2)\n", - "handle_order(\"Bob\", \"Smartphone\", 1)\n" + "# 5. Define print_order_statistics\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(f\"Total products ordered: {total}\")\n", + " print(f\"Percentage of unique products ordered: {percentage:.2f}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e3ba48f3", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# 6. Define print_updated_inventory\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2eb27461", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# 7. Main execution flow\n", + "products = [\"apple\", \"banana\", \"orange\", \"grape\", \"mango\"]\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\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)\n" ] } ],