From 4c19a737b5cd3f74212171d8f9938dbcc69ea2b2 Mon Sep 17 00:00:00 2001 From: loreS23-dot Date: Sat, 4 Oct 2025 16:26:14 +0200 Subject: [PATCH 1/2] lab3 resolution --- lab-python-functions.ipynb | 126 +++++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 5 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..0545007 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -17,9 +17,9 @@ "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", + "#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", + "#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", @@ -39,7 +39,123 @@ "\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", + "- Test your functions individually to ensure they work correctly.#\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "7078b47b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventario: {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n", + "Your order is completed. The order contains {'hat'}.\n", + "Costumers orders: {'hat'}\n", + "Updated inventory: {'t-shirt': 5, 'mug': 5, 'hat': 4, 'book': 5, 'keychain': 5}\n", + "Total products ordered: 1\n", + "Total products ordered: 1\n", + "Percentage of products ordered: 20.00%\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 5\n", + "hat: 4\n", + "book: 5\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " if quantity > 0:\n", + " inventory[product] = quantity\n", + " break\n", + " else:\n", + " print(\"Quantity must be a positive integer. Please try again.\")\n", + " return inventory\n", + "\n", + "inventory = initialize_inventory(products)\n", + "print(\"Inventario: \", inventory)\n", + " \n", + "\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(\"Enter a product to order (t-shirt, mug, hat, book, keychain): \").strip().lower()\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " while True: \n", + " o = input(\"Do you want to order another product? (yes/no): \").strip().lower() \n", + " if o == \"no\":\n", + " print(f\"Your order is completed. The order contains {customer_orders}.\")\n", + " return customer_orders \n", + " elif o == \"yes\":\n", + " break \n", + " else:\n", + " print(\"Invalid input. Please enter 'yes' or 'no'.\") \n", + " else:\n", + " print(\"Sorry. That prodcut is not aviable.\") \n", + " \n", + "costumers_orders = get_customer_orders()\n", + "print(\"Costumers orders: \", costumers_orders)\n", + "\n", + "\n", + "\n", + "def update_inventory(costumers_orders, inventory):\n", + " for order in costumers_orders:\n", + " if order in inventory and inventory[order] > 0:\n", + " inventory[order] -= 1\n", + " else:\n", + " print(f\"Sorry, {order} is out of stock.\")\n", + " return inventory\n", + "\n", + "inventory = update_inventory(costumers_orders, inventory)\n", + "print(\"Updated inventory: \", inventory)\n", + "\n", + "\n", + "def calculate_order_statistics(costumers_orders, products):\n", + " total_products_ordered = len(costumers_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "total_products_ordered, percentage_ordered = calculate_order_statistics(costumers_orders, products)\n", + "print(f\"Total products ordered: {total_products_ordered}\")\n", + "\n", + "def print_order_stadistics(order_stadistics):\n", + " total_products_ordered, percentage_ordered = order_stadistics\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of products ordered: {percentage_ordered:.2f}%\")\n", + "\n", + "print_order_stadistics((total_products_ordered, percentage_ordered))\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", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9740e928", + "metadata": {}, + "outputs": [], + "source": [ "\n", "\n" ] @@ -47,7 +163,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +177,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4, From 360f7af8689865b7c5505c9e8616141aa97cd317 Mon Sep 17 00:00:00 2001 From: loreS23-dot Date: Tue, 4 Nov 2025 17:56:37 +0100 Subject: [PATCH 2/2] functions lab --- lab-python-functions.ipynb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 0545007..81514e0 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -46,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "7078b47b", "metadata": {}, "outputs": [ @@ -54,19 +54,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "Inventario: {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n", - "Your order is completed. The order contains {'hat'}.\n", - "Costumers orders: {'hat'}\n", - "Updated inventory: {'t-shirt': 5, 'mug': 5, 'hat': 4, 'book': 5, 'keychain': 5}\n", - "Total products ordered: 1\n", - "Total products ordered: 1\n", - "Percentage of products ordered: 20.00%\n", + "\n", + "Order Statistics:\n", + "Total products ordered: 2\n", + "Percentage of unique products ordered: 40.00%\n", + "\n", "Updated Inventory:\n", - "t-shirt: 5\n", - "mug: 5\n", - "hat: 4\n", - "book: 5\n", - "keychain: 5\n" + "t-shirt: 10\n", + "mug: 10\n", + "hat: 9\n", + "book: 9\n", + "keychain: 10\n" ] } ],