From 17ff6489be77557d1a260575a0da1904d53fa354 Mon Sep 17 00:00:00 2001 From: FRILLAY Emilie Date: Thu, 21 May 2026 18:09:42 +0200 Subject: [PATCH] lab-python-functions.ipynb --- lab-python-functions.ipynb | 160 +++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..2a6db1f 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,166 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ff82e3aa", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #list of products" + ] + }, + { + "cell_type": "markdown", + "id": "f1ca1a50", + "metadata": {}, + "source": [ + "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. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0fa93ccc", + "metadata": {}, + "outputs": [], + "source": [ + "# Recall : create an empty dictionary called inventory -----> inventory = {} # dic_name = {}\n", + "def initialize_inventory (products): #----> Declare the function using the keyword\n", + " inventory = {}\n", + " for product in products:# for loop ----> take each product of the list\n", + "\n", + " quantity = int(\n", + " input(\n", + " f\"Enter the quantity of {product}: \"\n", + " )\n", + " )\n", + "\n", + " inventory[product] = quantity\n", + " return inventory #---->Return the completed dictionary so that the rest of the main program can use it.\n" + ] + }, + { + "cell_type": "markdown", + "id": "7f99bd7c", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a0f22277", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders ():\n", + " customer_orders = set()\n", + " for _ in range(3): # for loop\n", + " product_name = input(\"Enter a product name to order (e.g., t-shirt, mug, hat, book, keychain): \")\n", + " customer_orders.add(product_name) #modifying a set ----> # Adding elements to a set e.g fruit.add('grape')\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "markdown", + "id": "e31a9551", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "30f439d4", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory (customer_orders, inventory):\n", + " for product in products:\n", + " inventory[product] -= 1 \n" + ] + }, + { + "cell_type": "markdown", + "id": "f94372e9", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f7b021a5", + "metadata": {}, + "outputs": [], + "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", + " return total_products_ordered , percentage_ordered\n" + ] + }, + { + "cell_type": "markdown", + "id": "5512ce5c", + "metadata": {}, + "source": [ + "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**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1051cb6f", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics (order_statistics)\n", + "print(order_statistics)\n" + ] + }, + { + "cell_type": "markdown", + "id": "fb8c6a32", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92132433", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory (inventory):\n", + " for product in products:\n", + " inventory[product] -= 1\n" + ] + }, + { + "cell_type": "markdown", + "id": "444ff547", + "metadata": {}, + "source": [ + "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", + " inventory.items()" + ] } ], "metadata": {