From dcd934174165f65c0a56ccce246ab16789004c0c Mon Sep 17 00:00:00 2001 From: Julian Fernandes Date: Fri, 16 Jan 2026 10:49:16 +0100 Subject: [PATCH] Julian lab done --- lab-python-functions.ipynb | 398 ++++++++++++++++++++++++++++++++++++- 1 file changed, 396 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..6d510d8 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,405 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "739962da", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory is the following after the input: {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n", + "\n", + "Customer oders: {'book', 'mug'}\n", + "\n", + "Order Statistics:\n", + "Total products ordered: 2\n", + "Percentage of Products Ordered: 40.0\n", + "None\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 5 remaining\n", + "mug: 5 remaining\n", + "hat: 5 remaining\n", + "book: 5 remaining\n", + "keychain: 5 remaining\n" + ] + } + ], + "source": [ + "#clean code copied from below \n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "#1. \n", + "\n", + "def initialize_inventory(products):\n", + " \"\"\"function for inventory count for each of \n", + " the products in the list\"\"\"\n", + " inventory = {}\n", + " for item in products:\n", + " quantity = int(input(f\"Please insert inventory count for {item}: \"))\n", + " inventory[item] = quantity\n", + " return inventory\n", + "\n", + "#2.\n", + "\n", + "def get_customer_orders():\n", + " \"\"\"collect customer orders and add them to a set\"\"\"\n", + " customer_orders = set()\n", + " \n", + " while True:\n", + " product_name = input(\"Name of porduct customer wants to order: \")\n", + " customer_orders.add(product_name)\n", + "\n", + " while True:\n", + " continue_choice = input(f\"do you want to add another porduct? yes/no\")\n", + " \n", + " if continue_choice == \"yes\": \n", + " break\n", + " elif continue_choice == \"no\":\n", + " return customer_orders\n", + " else:\n", + " print(f\"invalid input, please enter yes or no\")\n", + "\n", + " if continue_choice == \"no\":\n", + " break\n", + "\n", + "#3\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"update inventory based on customer orders\"\"\"\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + "\n", + "\n", + "#4.\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"Calculating the statistics for the customer orders\"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "\n", + "\n", + "#5.\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"Printing the order statistics\"\"\"\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + "\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered}\")\n", + "\n", + "#6. \n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"Print the updated inventory.\"\"\"\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity} remaining\")\n", + "\n", + "\n", + "#all the calls to connect the functions \n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "#all the printed infomation\n", + "print(f\"Inventory is the following after the input: {inventory}\\n\")\n", + "print(f\"Customer oders: {customer_orders}\\n\")\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "print(f\"{print_order_statistics(order_statistics)}\\n\")\n", + "\n", + "print_updated_inventory(inventory)\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d016bcde", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "661a33a5", + "metadata": {}, + "source": [ + "______________________________\n", + "Split code for each step below" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "af566f62", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory is the following after the input: {'t-shirt': 6, 'mug': 6, 'hat': 7, 'book': 7, 'keychain': 7}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "#1. \n", + "\n", + "def initialize_inventory(products):\n", + " \"\"\"function for inventory count for each of \n", + " the products in the list\"\"\"\n", + " inventory = {}\n", + " for item in products:\n", + " quantity = int(input(f\"Please insert inventory count for {item}: \"))\n", + " inventory[item] = quantity\n", + " return inventory\n", + "\n", + "inventory = initialize_inventory(products)\n", + "print(f\"Inventory is the following after the input: {inventory}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "3267e322", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer oders: {'mug', 'keychain'}\n" + ] + } + ], + "source": [ + "#2.\n", + "\n", + "def get_customer_orders():\n", + " \"\"\"collect customer orders and add them to a set\"\"\"\n", + " customer_orders = set()\n", + " \n", + " while True:\n", + " product_name = input(\"Name of porduct customer wants to order: \")\n", + " customer_orders.add(product_name)\n", + "\n", + " while True:\n", + " continue_choice = input(f\"do you want to add another porduct? yes/no\")\n", + " \n", + " if continue_choice == \"yes\": \n", + " break\n", + " elif continue_choice == \"no\":\n", + " return customer_orders\n", + " else:\n", + " print(f\"invalid input, please enter yes or no\")\n", + "\n", + " if continue_choice == \"no\":\n", + " break\n", + "\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(f\"Customer oders: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "b7df7386", + "metadata": {}, + "outputs": [], + "source": [ + "#3\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"update inventory based on customer orders\"\"\"\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "77c53691", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'keychain'}\n", + "{'t-shirt': 6, 'mug': 5, 'hat': 7, 'book': 6, 'keychain': 7}\n" + ] + } + ], + "source": [ + "#checking code for 3.\n", + "\n", + "print(customer_orders)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4b63f344", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 6, 'mug': 4, 'hat': 7, 'book': 6, 'keychain': 6}\n" + ] + } + ], + "source": [ + "#checking code for 3\n", + "\n", + "update_inventory(customer_orders, inventory)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "a5d71237", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 40.0)" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#4.\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"Calculating the statistics for the customer orders\"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "79ae8d9b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total products ordered: 2\n", + "Percentage of Products Ordered: 40.0\n" + ] + } + ], + "source": [ + "#5.\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"Printing the order statistics\"\"\"\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + "\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered}\")\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "ae40f88c", + "metadata": {}, + "outputs": [], + "source": [ + "#6. \n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"Print the updated inventory.\"\"\"\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity} remaining\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "37dff377", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 6 remaining\n", + "mug: 4 remaining\n", + "hat: 7 remaining\n", + "book: 6 remaining\n", + "keychain: 6 remaining\n" + ] + } + ], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "data-analytics", "language": "python", "name": "python3" }, @@ -61,7 +455,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.23" } }, "nbformat": 4,