diff --git a/lab-python-functions-solved.ipynb b/lab-python-functions-solved.ipynb new file mode 100644 index 0000000..6b4bfb7 --- /dev/null +++ b/lab-python-functions-solved.ipynb @@ -0,0 +1,251 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lab | Functions\n", + "\n", + "## Managing Customer Orders with Functions\n", + "\n", + "This notebook refactors the previous customer-order program into reusable functions." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The products used in the previous lab are kept here so that the new functions reproduce the same behavior." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. Initialize the inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " \"\"\"Create an inventory dictionary with quantities entered by the user.\"\"\"\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Insert quantity of {product}: \"))\n", + " if quantity < 0:\n", + " print(\"Please enter a non-negative quantity.\")\n", + " continue\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a whole number.\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. Get customer orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " \"\"\"Collect valid, unique customer orders until the user chooses to stop.\"\"\"\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " order = input(\"Write the product you want: \").strip()\n", + "\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Item not found\")\n", + "\n", + " continue_order = input(\"Do you want to continue? yes/no: \").strip().lower()\n", + " if continue_order == \"no\":\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. Update the inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"Reduce the inventory by one unit for every ordered product.\"\"\"\n", + " for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] -= 1\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4. Calculate order statistics" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"Calculate the total and percentage of unique products ordered.\"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + "\n", + " if len(products) == 0:\n", + " percentage_unique_products_ordered = 0.0\n", + " else:\n", + " percentage_unique_products_ordered = (\n", + " total_products_ordered / len(products) * 100\n", + " )\n", + "\n", + " return total_products_ordered, percentage_unique_products_ordered" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 5. Print the order statistics" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " \"\"\"Display the order statistics.\"\"\"\n", + " total_products_ordered, percentage_unique_products_ordered = order_statistics\n", + "\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(\n", + " f\"Percentage of unique products ordered: \"\n", + " f\"{percentage_unique_products_ordered:.2f}%\"\n", + " )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6. Print the updated inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " \"\"\"Display every product and its remaining quantity.\"\"\"\n", + " print(\"Updated inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Execute the program\n", + "\n", + "Set RUN_INTERACTIVE to True when you want to enter the quantities and orders yourself. It is False by default so the notebook can be run without stopping for input." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "RUN_INTERACTIVE = False\n", + "\n", + "if RUN_INTERACTIVE:\n", + " inventory = initialize_inventory(products)\n", + " customer_orders = get_customer_orders()\n", + " update_inventory(customer_orders, inventory)\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(order_statistics)\n", + " print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example execution\n", + "\n", + "This example checks the complete sequence without requiring manual input." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "example_inventory = {product: 2 for product in products}\n", + "example_orders = {\"mug\", \"book\", \"hat\"}\n", + "\n", + "update_inventory(example_orders, example_inventory)\n", + "example_statistics = calculate_order_statistics(example_orders, products)\n", + "print_order_statistics(example_statistics)\n", + "print_updated_inventory(example_inventory)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb deleted file mode 100644 index 44d337b..0000000 --- a/lab-python-functions.ipynb +++ /dev/null @@ -1,69 +0,0 @@ -{ - "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 -}