From 233c15adcdf9ac65d4925f44794b44cc803f32aa Mon Sep 17 00:00:00 2001 From: Ofelia Date: Wed, 18 Feb 2026 18:11:45 +0100 Subject: [PATCH] Solved Lab --- .../lab-python-functions-checkpoint.ipynb | 284 ++++++++++++++++++ lab-python-functions.ipynb | 221 +++++++++++++- 2 files changed, 502 insertions(+), 3 deletions(-) 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..37493ab --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,284 @@ +{ + "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": 42, + "id": "2f082975-c13c-40f8-ad5d-cd523d02ddcb", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity available for t-shirt: 6\n", + "Enter quantity available for mug: 4\n", + "Enter quantity available for hat: 7\n", + "Enter quantity available for book: 3\n", + "Enter quantity available for keychain: 7\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 3, 'keychain': 7}" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "def initialize_inventory(products):\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity available for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "73a30797-223d-46ea-a2e7-cf02af84d2e2", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: mug\n", + "Enter the product name: hat\n", + "Enter the product name: book\n", + "Enter the product name: shirt\n", + "Enter the product name: keychain\n" + ] + }, + { + "data": { + "text/plain": [ + "{'book', 'hat', 'keychain', 'mug'}" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = set()\n", + "def get_customer_orders():\n", + " for product in products:\n", + " product_name = input(\"Enter the product name: \")\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " return customer_orders\n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "71947fed-ff05-45a0-9f52-ef680b0c848b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 2, 'keychain': 7}" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] -= 1\n", + " return inventory\n", + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "d792af9a-12e6-4b00-8aea-0f1e655d9b68", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 80.0)" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_ordered = (len(customer_orders) / len(products)) * 100\n", + " order_statistics = (total_products_ordered, percentage_unique_ordered)\n", + " return order_statistics\n", + "calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "78162442-210e-49c7-b37f-c9f94f240c1a", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: (4, 80.0)\n" + ] + } + ], + "source": [ + "def print_order_statistics(order_statistics):\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print(f\"Order statistics: {order_statistics}\")\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "f580057a-f0d0-4ac6-9253-b3f2cf3c899e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: {'t-shirt': 6, 'mug': 7, 'hat': 4, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " updated_inventory = update_inventory(customer_orders, inventory)\n", + " print(f\"Updated inventory: {updated_inventory}\")\n", + "print_updated_inventory(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "4f354bcc-7d4c-447a-aa2a-8a93971d2994", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity available for t-shirt: 7\n", + "Enter quantity available for mug: 4\n", + "Enter quantity available for hat: 8\n", + "Enter quantity available for book: 9\n", + "Enter quantity available for keychain: 5\n", + "Enter the product name: mug\n", + "Enter the product name: hat\n", + "Enter the product name: shirt\n", + "Enter the product name: book\n", + "Enter the product name: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: (4, 80.0)\n", + "Updated inventory: {'t-shirt': 7, 'mug': 4, 'hat': 8, 'book': 7, 'keychain': 5}\n" + ] + } + ], + "source": [ + "initialize_inventory(products)\n", + "get_customer_orders()\n", + "calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory) " + ] + } + ], + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..37493ab 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,228 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "2f082975-c13c-40f8-ad5d-cd523d02ddcb", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity available for t-shirt: 6\n", + "Enter quantity available for mug: 4\n", + "Enter quantity available for hat: 7\n", + "Enter quantity available for book: 3\n", + "Enter quantity available for keychain: 7\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 3, 'keychain': 7}" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "def initialize_inventory(products):\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity available for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "73a30797-223d-46ea-a2e7-cf02af84d2e2", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product name: mug\n", + "Enter the product name: hat\n", + "Enter the product name: book\n", + "Enter the product name: shirt\n", + "Enter the product name: keychain\n" + ] + }, + { + "data": { + "text/plain": [ + "{'book', 'hat', 'keychain', 'mug'}" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = set()\n", + "def get_customer_orders():\n", + " for product in products:\n", + " product_name = input(\"Enter the product name: \")\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " return customer_orders\n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "71947fed-ff05-45a0-9f52-ef680b0c848b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 2, 'keychain': 7}" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] -= 1\n", + " return inventory\n", + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "d792af9a-12e6-4b00-8aea-0f1e655d9b68", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 80.0)" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_ordered = (len(customer_orders) / len(products)) * 100\n", + " order_statistics = (total_products_ordered, percentage_unique_ordered)\n", + " return order_statistics\n", + "calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "78162442-210e-49c7-b37f-c9f94f240c1a", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: (4, 80.0)\n" + ] + } + ], + "source": [ + "def print_order_statistics(order_statistics):\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print(f\"Order statistics: {order_statistics}\")\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "f580057a-f0d0-4ac6-9253-b3f2cf3c899e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory: {'t-shirt': 6, 'mug': 7, 'hat': 4, 'book': 5, 'keychain': 5}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " updated_inventory = update_inventory(customer_orders, inventory)\n", + " print(f\"Updated inventory: {updated_inventory}\")\n", + "print_updated_inventory(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "4f354bcc-7d4c-447a-aa2a-8a93971d2994", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity available for t-shirt: 7\n", + "Enter quantity available for mug: 4\n", + "Enter quantity available for hat: 8\n", + "Enter quantity available for book: 9\n", + "Enter quantity available for keychain: 5\n", + "Enter the product name: mug\n", + "Enter the product name: hat\n", + "Enter the product name: shirt\n", + "Enter the product name: book\n", + "Enter the product name: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order statistics: (4, 80.0)\n", + "Updated inventory: {'t-shirt': 7, 'mug': 4, 'hat': 8, 'book': 7, 'keychain': 5}\n" + ] + } + ], + "source": [ + "initialize_inventory(products)\n", + "get_customer_orders()\n", + "calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory) " + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -61,7 +276,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,