From c64fc9bb9ac619d02c3ea4f242d0b73161f5409b Mon Sep 17 00:00:00 2001 From: PARVINSUMI Date: Thu, 3 Jul 2025 09:56:32 +0200 Subject: [PATCH 1/2] solved lab function --- lab-python-functions.ipynb | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..a5dd9b7 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,37 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "092c0bd8-edce-4f02-a205-23595f0e7559", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1916f75-6c78-404d-9156-77ce8a445582", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a3c7661e-dbf7-427f-8c46-5d7acfce49bc", + "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 +85,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.2" } }, "nbformat": 4, From a25b87ff5d26f4d4bdf448f67c45235fd0c853dd Mon Sep 17 00:00:00 2001 From: PARVINSUMI Date: Thu, 3 Jul 2025 11:33:11 +0200 Subject: [PATCH 2/2] solved lab functions --- lab-python-functions.ipynb | 212 ++++++++++++++++++++++++++++++++++++- 1 file changed, 207 insertions(+), 5 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index a5dd9b7..87dff34 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -46,26 +46,228 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "092c0bd8-edce-4f02-a205-23595f0e7559", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#Define a function named initialize_inventory that takes products as a parameter. \n", + "#Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n", + "\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " if quantity < 0:\n", + " print(\"Quantity cannot be negative. Please try again.\")\n", + " continue\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a number.\")\n", + " return inventory\n", + "\n", + "\n" + ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "f1916f75-6c78-404d-9156-77ce8a445582", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#Define a function named get_customer_orders that takes no parameters. Inside the function, \n", + "#implement the code for prompting the user to enter the product names using a loop. \n", + "#The function should return the customer_orders set.\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " print(\"Enter the product names one by one. Type 'done' to finish.\")\n", + " \n", + " while True:\n", + " product = input(\"Enter product name: \").strip()\n", + " if product.lower() == 'done':\n", + " break\n", + " if product: # Only add non-empty strings\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"Product name cannot be empty.\")\n", + " \n", + " return customer_orders\n" + ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "a3c7661e-dbf7-427f-8c46-5d7acfce49bc", "metadata": {}, "outputs": [], + "source": [ + "#Define a function named update_inventory that takes customer_orders and inventory as parameters. \n", + "#Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " print(f\"{product} ordered. Remaining stock: {inventory[product]}\")\n", + " else:\n", + " print(f\"{product} is out of stock.\")\n", + " else:\n", + " print(f\"{product} is not found in the inventory.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4b68f068-21d6-4f4a-aebd-7d6f7bea60cc", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named calculate_order_statistics that takes customer_orders and products as parameters. \n", + "#Inside the function, implement the code for calculating the order statistics \n", + "#(total products ordered, and percentage of unique products ordered). The function should return these values.\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_ordered = len(customer_orders) # Total unique products ordered\n", + " unique_products = len(customer_orders) # Unique products in the order\n", + " total_products = len(products) # Total available products in the catalog\n", + " \n", + " # Calculate the percentage of unique products ordered\n", + " percentage_unique = (unique_products / total_products) * 100 if total_products > 0 else 0\n", + " \n", + " return total_ordered, percentage_unique\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "48ae953f-982e-4e4e-918b-7859c1be340e", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named print_order_statistics that takes order_statistics as a parameter. \n", + "#Inside the function, implement the code for printing the order statistics.\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_ordered, percentage_unique = order_statistics\n", + " print(f\"Total products ordered: {total_ordered}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_unique:.2f}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1301a78d-182b-4c1a-9bcb-1ea22987f06b", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named print_updated_inventory that takes inventory as a parameter. \n", + "#Inside the function, implement the code for printing the updated inventory.\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "116bb077-4807-4ea5-a14e-af77ff82156c", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for Apples: 4\n", + "Enter quantity for Bananas: 4\n", + "Enter quantity for Oranges: 4\n", + "Enter quantity for Grapes: 4\n", + "Enter quantity for Peaches: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the product names one by one. Type 'done' to finish.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product name: 4\n", + "Enter product name: apple\n", + "Enter product name: banana\n", + "Enter product name: done\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple is not found in the inventory.\n", + "4 is not found in the inventory.\n", + "banana is not found in the inventory.\n", + "Total products ordered: 3\n", + "Percentage of unique products ordered: 60.00%\n", + "Updated Inventory:\n", + "Apples: 4\n", + "Bananas: 4\n", + "Oranges: 4\n", + "Grapes: 4\n", + "Peaches: 4\n" + ] + } + ], + "source": [ + "#Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "\n", + "\n", + "# Main program execution\n", + "def main():\n", + " # Example products available in the store\n", + " products = ['Apples', 'Bananas', 'Oranges', 'Grapes', 'Peaches']\n", + " \n", + " # Step 1: Initialize the inventory\n", + " inventory = initialize_inventory(products)\n", + " \n", + " # Step 2: Get customer orders\n", + " customer_orders = get_customer_orders()\n", + " \n", + " # Step 3: Update the inventory based on customer orders\n", + " update_inventory(customer_orders, inventory)\n", + " \n", + " # Step 4: Calculate order statistics\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " \n", + " # Step 5: Print order statistics\n", + " print_order_statistics(order_statistics)\n", + " \n", + " # Step 6: Print updated inventory\n", + " print_updated_inventory(inventory)\n", + "\n", + "# Execute the main program\n", + "if __name__ == \"__main__\":\n", + " main()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9aeacc37-f0fb-46c7-88f7-b9f97ccee651", + "metadata": {}, + "outputs": [], "source": [] } ],