From c5f7280b7dd2dec5e2d9150b648d2885d62aa0b3 Mon Sep 17 00:00:00 2001 From: Sofija Date: Wed, 16 Sep 2026 15:12:44 +0200 Subject: [PATCH] Solved lab --- lab-python-functions.ipynb | 473 ++++++++++++++++++++++++++++++++++++- 1 file changed, 472 insertions(+), 1 deletion(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..bcd07c3 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,477 @@ "\n", "\n" ] + }, + { + "cell_type": "markdown", + "id": "829640c3-7812-4c25-854a-c64590e0d567", + "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": 11, + "id": "81b8d593-ea84-40fc-8f54-6ca9c871133e", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "afb0831e-b379-45eb-a067-d122caaf8b5b", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " for item in products:\n", + " quantity = int(input(f\"Enter the number of {item}: \"))\n", + " inventory[item] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "566f271c-0557-49e5-a52e-f41e9b39ba12", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of t-shirt: 3\n", + "Enter the number of mug: 2\n", + "Enter the number of hat: 3\n", + "Enter the number of book: 2\n", + "Enter the number of keychain: 3\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 3, 'mug': 2, 'hat': 3, 'book': 2, 'keychain': 3}" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "markdown", + "id": "ceac4111-7676-4901-91b9-d2d9baca2e30", + "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": 47, + "id": "3aa62cbf-7354-444d-957c-0db1bca55df1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " \n", + " customer_orders = set()\n", + " add_more = \"yes\"\n", + " \n", + " while add_more == \"yes\":\n", + " order = input(f\"Name 1 product from the list {products}: \")\n", + " \n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(f\"The item '{order}' is not in the list of products.\")\n", + " \n", + " add_more = input(\"Do you want to add another product (yes/no)? \")\n", + "\n", + " while add_more not in [\"yes\", \"no\"]:\n", + " print(\"Invalid response\")\n", + " add_more = input(\"Do you want to add another product (yes/no)? \")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "b5a30110-88f2-4f54-a3de-467fb8b25477", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Name 1 product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: hat\n", + "Do you want to add another product (yes/no)? yes\n", + "Name 1 product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: book\n", + "Do you want to add another product (yes/no)? yes\n", + "Name 1 product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: keychain\n", + "Do you want to add another product (yes/no)? no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'book', 'keychain'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "b276f72b-dee7-45d6-975a-1d40afd1a8d4", + "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": 53, + "id": "541458af-5992-4e71-8563-1b970300ac11", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders,inventory):\n", + " for item in customer_orders:\n", + " inventory[item] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "b8e1d8db-04b4-49f1-a468-f032622d93ca", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 3, 'mug': 2, 'hat': 2, 'book': 1, 'keychain': 2}" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders,inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "aaef2be4-4972-45b9-bfa2-fd00251d056d", + "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.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "282958a2-97d8-432f-a7e2-0519bd971935", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_orders = len(customer_orders)\n", + " percentage = (total_orders / len(products) * 100)\n", + " return total_orders, percentage" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "b11bce06-a3b9-4995-8c13-e97c738eea17", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(order_statistics)" + ] + }, + { + "cell_type": "markdown", + "id": "4f9b0ea7-cf72-446d-9c46-f8e983bbce4c", + "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": 61, + "id": "d26fb883-1704-4272-95b4-01cd95e10cea", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(f\"\"\"Order Statistics:\n", + " Total Products Ordered: {order_statistics[0]}\n", + " Percentage of Products Ordered: {order_statistics[1]}%\"\"\")\n", + " return print" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "3eb4ea21-be9b-4ade-a5d8-9076fc00d26b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + " Total Products Ordered: 3\n", + " Percentage of Products Ordered: 60.0%\n", + "\n" + ] + } + ], + "source": [ + "print(print_order_statistics(order_statistics))" + ] + }, + { + "cell_type": "markdown", + "id": "d2968a3f-8f4a-45c1-8b88-bd5fbab08ce2", + "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": 84, + "id": "df9f3a95-d51b-4d50-9d02-d735a5ddddb6", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " for product, quantity in inventory.items():\n", + " print(f\"In stock - {product}: {quantity} item(s)\")" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "245926b3-3dae-4f52-89cc-ad6e08f5e7ea", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "In stock - t-shirt: 3 item(s)\n", + "In stock - mug: 2 item(s)\n", + "In stock - hat: 2 item(s)\n", + "In stock - book: 1 item(s)\n", + "In stock - keychain: 2 item(s)\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "4ccd8162-3ae4-4727-b479-33358a77017e", + "metadata": {}, + "source": [ + "7. Call the functions in the appropriate sequence to execute the program and manage customer orders." + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "8167ed68-6d53-4623-9484-1d91a771b936", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of t-shirt: 3\n", + "Enter the number of mug: 5\n", + "Enter the number of hat: 6\n", + "Enter the number of book: 9\n", + "Enter the number of keychain: 8\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 3, 'mug': 5, 'hat': 6, 'book': 9, 'keychain': 8}" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "3d9bad31-eb6a-4467-99c4-ba21ca1f4af9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Name 1 product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: mug\n", + "Do you want to add another product (yes/no)? yes\n", + "Name 1 product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: hat\n", + "Do you want to add another product (yes/no)? yes\n", + "Name 1 product from the list ['t-shirt', 'mug', 'hat', 'book', 'keychain']: book\n", + "Do you want to add another product (yes/no)? no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "885486b8-1cd0-470e-87c0-7facaf2c1052", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 3, 'mug': 4, 'hat': 5, 'book': 8, 'keychain': 8}" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders,inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "bb1dce43-e317-4780-9d9b-9d4afd8f5400", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "f1a17fc2-6af3-4e92-bb46-8337694df8d1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + " Total Products Ordered: 3\n", + " Percentage of Products Ordered: 60.0%\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "14317de2-8e71-465c-9b27-75d6a8c965b7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "In stock - t-shirt: 3 item(s)\n", + "In stock - mug: 4 item(s)\n", + "In stock - hat: 5 item(s)\n", + "In stock - book: 8 item(s)\n", + "In stock - keychain: 8 item(s)\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fce656dd-1c4f-44c5-8c81-11083b600bb9", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +532,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.7" } }, "nbformat": 4,