From 54b5cc1059bd989f61d0e3a705c3944e668dfdba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Tue, 14 Apr 2026 18:00:39 +0200 Subject: [PATCH 1/2] Update lab-python-functions.ipynb Test functions --- lab-python-functions.ipynb | 91 +++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..28ffaf9 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,98 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b949e30f", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity of {product}: \").strip())\n", + " inventory[product] = quantity\n", + " \n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " print (\"Available products\", products)\n", + " while True:\n", + " order = input('Enter the name of the product to purchase: ').strip().lower()\n", + " \n", + " while order not in products:\n", + " print (f\"'{order}' is not in our inventory. Please write it correctly.\")\n", + " order = input('Enter the name of the product to purchase: ').strip().lower()\n", + " \n", + " customer_orders.add(order)\n", + "\n", + " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", + "\n", + " while validation not in (\"no\", \"yes\"):\n", + " print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\")\n", + " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", + "\n", + " if validation == \"no\":\n", + " break\n", + "\n", + " print (f\"The customer has ordered {customer_orders}\" \"\\n\")\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders,inventory):\n", + " for item in customer_orders:\n", + " inventory[item] -= 1\n", + " return inventory\n", + "\n", + "def calculate_order_statistics(customer_orders,products):\n", + "\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", + "def print_order_statistics(order_statistics):\n", + "\n", + " total_products_ordered, percentage_of_products_ordered = order_statistics\n", + " print (\"Order Statistics:\")\n", + " print (f\"Total Products Ordered: {total_products_ordered}\")\n", + " print (f\"Percentage of Products Ordered : {percentage_of_products_ordered}%\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " for key,value in inventory.items():\n", + " print (f\"The current quantity of item '{key}' is: {value}\")\n", + "\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products) \n", + "customer_orders = get_customer_orders()\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "order_statistics = calculate_order_statistics(customer_orders,products)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d42e1026", + "metadata": {}, + "outputs": [], + "source": [ + "1" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +148,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.4" } }, "nbformat": 4, From 3228c26853cd46ea25b3103f97542b53501c7f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Tue, 14 Apr 2026 23:46:12 +0200 Subject: [PATCH 2/2] Update lab-python-functions.ipynb test functions --- lab-python-functions.ipynb | 117 +++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 57 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 28ffaf9..fc91a59 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -51,85 +51,88 @@ "metadata": {}, "outputs": [], "source": [ - "def initialize_inventory(products):\n", - " inventory = {}\n", - " for product in products:\n", - " quantity = int(input(f\"Enter quantity of {product}: \").strip())\n", - " inventory[product] = quantity\n", - " \n", - " return inventory\n", + "def manage_orders():\n", + " def initialize_inventory(products):\n", + " inventory = {}\n", "\n", - "def get_customer_orders():\n", - " customer_orders = set()\n", - "\n", - " print (\"Available products\", products)\n", - " while True:\n", - " order = input('Enter the name of the product to purchase: ').strip().lower()\n", - " \n", - " while order not in products:\n", - " print (f\"'{order}' is not in our inventory. Please write it correctly.\")\n", - " order = input('Enter the name of the product to purchase: ').strip().lower()\n", + " for product in products:\n", + " quantity = input(f\"Enter quantity of {product}: \").strip()\n", " \n", - " customer_orders.add(order)\n", + " while not quantity.isdigit():\n", + " print(\"Please enter a number\")\n", + " quantity = input(f\"Enter quantity of {product}: \").strip()\n", "\n", - " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", + " quantity = int(quantity)\n", + " inventory[product] = quantity\n", + " \n", + " return inventory\n", "\n", - " while validation not in (\"no\", \"yes\"):\n", - " print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\")\n", - " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", + " def get_customer_orders():\n", + " customer_orders = set()\n", "\n", - " if validation == \"no\":\n", - " break\n", + " while True:\n", + " order = input('Enter the name of the product to purchase: ').strip().lower()\n", + " \n", + " while order not in products:\n", + " print (f\"'{order}' is not in our inventory. Please write it correctly.\")\n", + " order = input('Enter the name of the product to purchase: ').strip().lower()\n", + " \n", + " customer_orders.add(order)\n", "\n", - " print (f\"The customer has ordered {customer_orders}\" \"\\n\")\n", - " return customer_orders\n", + " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", "\n", - "def update_inventory(customer_orders,inventory):\n", - " for item in customer_orders:\n", - " inventory[item] -= 1\n", - " return inventory\n", + " while validation not in (\"no\", \"yes\"):\n", + " print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\")\n", + " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", "\n", - "def calculate_order_statistics(customer_orders,products):\n", + " if validation == \"no\":\n", + " break\n", "\n", - " total_products_ordered = len(customer_orders)\n", - " percentage_ordered = (total_products_ordered/len(products))*100\n", + " print (f\"The customer has ordered {customer_orders}\" \"\\n\")\n", + " return customer_orders\n", "\n", - " return total_products_ordered, percentage_ordered\n", + " def update_inventory(customer_orders,inventory):\n", + " for item in customer_orders:\n", + " inventory[item] -= 1\n", + " return inventory\n", "\n", - "def print_order_statistics(order_statistics):\n", + " def calculate_order_statistics(customer_orders,products):\n", "\n", - " total_products_ordered, percentage_of_products_ordered = order_statistics\n", - " print (\"Order Statistics:\")\n", - " print (f\"Total Products Ordered: {total_products_ordered}\")\n", - " print (f\"Percentage of Products Ordered : {percentage_of_products_ordered}%\")\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = round((total_products_ordered/len(products))*100,1)\n", "\n", - "def print_updated_inventory(inventory):\n", - " for key,value in inventory.items():\n", - " print (f\"The current quantity of item '{key}' is: {value}\")\n", + " return total_products_ordered, percentage_ordered\n", "\n", + " def print_order_statistics(order_statistics):\n", "\n", - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " total_products_ordered, percentage_of_products_ordered = order_statistics\n", + " print(\"------Order Statistics------\")\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of products ordered: {percentage_of_products_ordered}%\")\n", "\n", - "inventory = initialize_inventory(products) \n", - "customer_orders = get_customer_orders()\n", + " def print_updated_inventory(inventory):\n", + " print(\"------Updated inventory------\")\n", + " for key,value in inventory.items():\n", + " print(f\"The current quantity of item '{key}' is: {value}\")\n", "\n", - "inventory = update_inventory(customer_orders, inventory)\n", - "order_statistics = calculate_order_statistics(customer_orders,products)\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", - "print_order_statistics(order_statistics)\n", - "print_updated_inventory(inventory)\n", - "\n" + " inventory = initialize_inventory(products) \n", + " print (\"Available products\", products)\n", + " print()\n", + " customer_orders = get_customer_orders()\n", + " inventory = update_inventory(customer_orders, inventory)\n", + " order_statistics = calculate_order_statistics(customer_orders,products)\n", + " print_order_statistics(order_statistics)\n", + " print()\n", + " print_updated_inventory(inventory)" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "d42e1026", + "cell_type": "markdown", + "id": "703a979d", "metadata": {}, - "outputs": [], - "source": [ - "1" - ] + "source": [] } ], "metadata": {