From 69f279a85c62791d1c1f9048ad11ff2fc585b7c2 Mon Sep 17 00:00:00 2001 From: Aline Granells Date: Thu, 18 Jun 2026 14:35:00 +0100 Subject: [PATCH] Solved lab - Day 3 --- lab-python-functions.ipynb | 152 ++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..0ee9259 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,159 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "6df02b54", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Exercise number 1\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "6d493778", + "metadata": {}, + "outputs": [], + "source": [ + "# Exercise number 2\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " add_another = \"yes\"\n", + "\n", + " while add_another == \"yes\":\n", + " order = input(\"Please input the name of the product: \").lower().strip()\n", + " customer_orders.add(order)\n", + "\n", + " add_another = input(\"Do you want another product? (yes/no): \").lower().strip()\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "2e61470b", + "metadata": {}, + "outputs": [], + "source": [ + "# Exercise number 3\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" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "45690d3b", + "metadata": {}, + "outputs": [], + "source": [ + "# Exercise number 4\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_order = len(customer_orders)\n", + " rate = (total_order / len(products)) * 100\n", + "\n", + " return total_order, rate" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "e446f5a9", + "metadata": {}, + "outputs": [], + "source": [ + "# Exercise number 5\n", + "def print_order_statistics(order_statistics):\n", + " total_order, rate = order_statistics\n", + " \n", + " print(f\"Total Ordered: {total_order}\")\n", + " print(f\"Percentagem of products ordered: {rate}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "42a641d9", + "metadata": {}, + "outputs": [], + "source": [ + "# Exercise number 6\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated inventory: \")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "9932bcc3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Ordered: 1\n", + "Percentagem of products ordered: 20.0\n", + "\n", + "Updated inventory: \n", + "t-shirt: 10\n", + "mug: 10\n", + "hat: 10\n", + "book: 9\n", + "keychain: 10\n" + ] + } + ], + "source": [ + "# Exercise number 7\n", + "\n", + "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(customer_orders, products)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "01493c6b", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +209,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.5" } }, "nbformat": 4,