From 5defa8d0277b0f3a3efc22ba823d94a0c2f68cb5 Mon Sep 17 00:00:00 2001 From: juliana-therezo Date: Thu, 17 Sep 2026 11:11:51 +0100 Subject: [PATCH] Update lab-python-functions.ipynb --- lab-python-functions.ipynb | 149 ++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..163aaaa 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,156 @@ "\n", "\n" ] + }, + { + "cell_type": "markdown", + "id": "66203fb5", + "metadata": {}, + "source": [ + "#LAB Solution" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d9b6cb7a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 50, 'mug': 10, 'hat': 15, 'book': 25, 'keychain': 30}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\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", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "04d5b7b1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " product = input(\"Enter a product to order: \")\n", + " customer_orders.add(product)\n", + "\n", + " answer = input(\"Do you want to add another product? (Yes/No): \")\n", + "\n", + " if answer == \"No\":\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "577f1388", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + "\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"{product} is not available in the inventory.\") \n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "20163124", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100 if products else 0\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "6e23b89f", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(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:.1f}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "1a02e119", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + "\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c22e17ac", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(\n", + " customer_orders, products\n", + ")\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,