From 08790bc296deac74560f259ed451bbe847603b03 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 19 Feb 2026 17:36:33 +0100 Subject: [PATCH] solved lab --- lab-python-functions.ipynb | 241 ++++++++++++++++++++++++++++++++++++- 1 file changed, 240 insertions(+), 1 deletion(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..1041ece 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,245 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "1a03426d-e96e-453b-9a67-a7ff25b58965", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "products\n" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "45bcdcc8-9fec-4f4e-a1ef-939f0e111912", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for t-shirt: 10\n", + "Enter the quantity for mug: 20\n", + "Enter the quantity for hat: 30\n", + "Enter the quantity for book: 40\n", + "Enter the quantity for keychain: 50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}\n" + ] + } + ], + "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", + " \n", + " inventory = {}\n", + " \n", + " for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " \n", + " return inventory\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "f6b459b0-dfcd-4ab4-a8fe-89b69d8939c8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Choose a product to order : book\n", + "Do you want to order another product? (Yes/No) yes\n", + "Choose a product to order : hat\n", + "Do you want to order another product? (Yes/No) no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 'book'}\n" + ] + } + ], + "source": [ + "#Define a function named get_customer_orders that takes no parameters.\n", + "#Inside the function, 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", + "\n", + "\n", + "def get_customer_orders():\n", + " \n", + " customer_orders = set()\n", + " \n", + " product = input(\"Choose a product to order : \")\n", + " customer_orders.add(product)\n", + " question = input(\"Do you want to order another product? (Yes/No)\").lower()\n", + "\n", + " while question != \"no\":\n", + " product = input(\"Choose a product to order : \")\n", + " customer_orders.add(product)\n", + " question = input(\"Do you want to order another product? (Yes/No)\").lower()\n", + " \n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "0c56fc23-60d1-4686-a3fa-1e4cd5bc50ac", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt 10\n", + "mug 20\n", + "hat 29\n", + "book 39\n", + "keychain 50\n" + ] + } + ], + "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", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " \n", + " return inventory\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "for product, quantity in inventory.items():\n", + " print(product, quantity)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "fc64bba5-c119-4e81-9717-3f5592367934", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 40.0)\n" + ] + } + ], + "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).\n", + "#The function should return these values.\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_products_ordered = (total_products_ordered / len(products))*100\n", + "\n", + " return total_products_ordered, percentage_of_products_ordered\n", + "\n", + "stats = calculate_order_statistics(customer_orders, products)\n", + "print(stats)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "88026476-4273-4768-adde-20aa41f0861d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 40.0)\n" + ] + } + ], + "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(customer_orders, products):\n", + " stats = calculate_order_statistics(customer_orders, products)\n", + " print(stats)\n", + "\n", + "print_statistics = print_order_statistics(customer_orders, products) " + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "0ef50b7d-3282-46ba-8ee3-8bfb407c2f23", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 20, 'hat': 29, 'book': 39, 'keychain': 50}\n" + ] + } + ], + "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(inventory)\n", + "\n", + "updated_inventory = print_updated_inventory(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9ec38cbd-ae89-4389-a1ae-8db1d87f8d2c", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +300,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,