From 23ee0ff401405c072acac8ffefde11de0a0a0077 Mon Sep 17 00:00:00 2001 From: Safina E Jahan Date: Fri, 28 Nov 2025 23:21:42 +0100 Subject: [PATCH] solved_lab-python-functions --- .../lab-python-functions-checkpoint.ipynb | 69 +++++ anaconda_projects/db/project_filebrowser.db | Bin 0 -> 32768 bytes lab-python-functions.ipynb | 274 +++++++++++++++++- 3 files changed, 340 insertions(+), 3 deletions(-) create mode 100644 .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb create mode 100644 anaconda_projects/db/project_filebrowser.db diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..44d337b --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,69 @@ +{ + "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 +} diff --git a/anaconda_projects/db/project_filebrowser.db b/anaconda_projects/db/project_filebrowser.db new file mode 100644 index 0000000000000000000000000000000000000000..3fa3a4a02c0753b66dd6cf65930b505be899ade0 GIT binary patch literal 32768 zcmeI)PjAv-9Ki9GZT#Ch^u&qe$&zJ|Et)Z>@nB-b8W)iXE5^kvO$QIA2!%mGubW3t zdl9|}Ux^oAgh!9Igf2kl;=zNd--ch>=XvOp=lS%PCQn*8s+Jv3idJvXwLP(?+)`9k zc`SsYD64)X{fPS!^CRuYvLE55h6gw7SCu>C&zkm4Sy{VOGg{{B+NJho{kzuI+Bcj< z69NbzfB*srAbc%c7q{byY%ZsM8hQ3z(-g1uTIso76O}_#RO;pOj!37Pwr2~|cw+{I-Un9>u1c=mmD55~w(L>Iv!pwA z20gbcUGJ*5r^YkAUN*(n)=bBq{cbopS6nr}J(>MNI`yh{@KUcGixM|9wNuCD2TNeoj1! z#k1Az-{r(&XS)j(Oa9FySlcU_A8O`@;(yo>KmY**5I_I{1Q0*~0R#|0AR+?mYHYLh zVE^I%>Am~KU0Dns>a+fj$i-zE2q1s}0tg_000IagfB*srOa(Gao2l9LfAIX@f9anM z0R#|0009ILKmY**5I_I{1R^ZJ`ai-Km#HCu00IagfB*srAb|BuSmWF810fB*srAb= 0:\n", + " inventory[product] = quantity\n", + " break\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a positive number.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number.\")\n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + " \"\"\"\n", + " Prompts the user to enter product names for an order using a loop.\n", + " \n", + " :return: A set of unique product names ordered by the customer.\n", + " \"\"\"\n", + " customer_orders = set()\n", + " print(\"\\n--- Customer Order Entry ---\")\n", + " while True:\n", + " # a. Prompt the user to enter the name of a product\n", + " product_name = input(\"Enter a product to order (or type 'done' to finish): \").strip()\n", + " \n", + " if product_name.lower() == 'done':\n", + " break\n", + " \n", + " # Optional: Basic validation to check if the product is in the master list\n", + " if product_name in AVAILABLE_PRODUCTS:\n", + " # b. Add the product name to the \"customer_orders\" set\n", + " customer_orders.add(product_name)\n", + " print(f\"'{product_name}' added to the order set.\")\n", + " else:\n", + " print(f\"'{product_name}' is not an available product. Please try again.\")\n", + "\n", + " # c. Ask the user if they want to add another product (now implicit in 'done' break)\n", + " \n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Updates the inventory by subtracting 1 from the quantity of each product \n", + " that was ordered.\n", + " \n", + " :param customer_orders: The set of products ordered by the customer.\n", + " :param inventory: The dictionary representing current stock levels.\n", + " \"\"\"\n", + " print(\"\\n--- Updating Inventory ---\")\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " # Subtract 1 from the quantity for each unique ordered product\n", + " inventory[product] -= 1\n", + " print(f\"Inventory reduced for '{product}'. New quantity: {inventory[product]}\")\n", + " elif product in inventory and inventory[product] == 0:\n", + " print(f\"Warning: '{product}' was ordered but is out of stock.\")\n", + " else:\n", + " # Should not happen if validation in get_customer_orders is good\n", + " print(f\"Error: Ordered product '{product}' not found in inventory.\")\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"\n", + " Calculates the total number of unique products ordered and the percentage \n", + " of unique products ordered relative to the total number of available products.\n", + " \n", + " :param customer_orders: The set of unique products ordered.\n", + " :param products: The list of all available products.\n", + " :return: A tuple containing (total_products_ordered, percentage_ordered).\n", + " \"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + " total_available_products = len(products)\n", + " \n", + " if total_available_products > 0:\n", + " # Calculate percentage: (unique ordered / total available) * 100\n", + " percentage_ordered = (total_products_ordered / total_available_products) * 100\n", + " else:\n", + " percentage_ordered = 0.0\n", + " \n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"\n", + " Prints the calculated order statistics in a formatted way.\n", + " \n", + " :param order_statistics: A tuple containing (total_products_ordered, percentage_ordered).\n", + " \"\"\"\n", + " total_ordered, percentage = order_statistics\n", + " \n", + " print(\"\\n--- Order Statistics ---\")\n", + " print(f\"Total unique products ordered: **{total_ordered}**\")\n", + " print(f\"Percentage of unique available products ordered: **{percentage:.2f}%**\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"\n", + " Prints the final state of the inventory dictionary.\n", + " \n", + " :param inventory: The dictionary representing the final stock levels.\n", + " \"\"\"\n", + " print(\"\\n--- Final Updated Inventory ---\")\n", + " # Use a loop for clean, key-value printing\n", + " for product, quantity in inventory.items():\n", + " status = \" (LOW STOCK)\" if quantity < 5 else \"\"\n", + " print(f\"**{product}**: {quantity}{status}\")\n", + "\n", + "# --- Main Program Execution ---\n", + "\n", + "# 1. Initialize Inventory\n", + "inventory_data = initialize_inventory(AVAILABLE_PRODUCTS)\n", + "\n", + "# 2. Get Customer Orders\n", + "orders_set = get_customer_orders()\n", + "\n", + "# 3. Update Inventory\n", + "update_inventory(orders_set, inventory_data)\n", + "\n", + "# 4. Calculate Statistics\n", + "stats = calculate_order_statistics(orders_set, AVAILABLE_PRODUCTS)\n", + "\n", + "# 5. Print Statistics\n", + "print_order_statistics(stats)\n", + "\n", + "# 6. Print Updated Inventory\n", + "print_updated_inventory(inventory_data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1240279-8751-448f-b5fd-e9c00f67b9b4", + "metadata": {}, + "outputs": [], + "source": [] } ], "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 +329,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,