From ce52672b3167751c119a8790295fc9e2fcc5818b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Gomes?= Date: Fri, 10 Oct 2025 18:47:44 +0200 Subject: [PATCH] Done with the Functions Lab --- lab-python-functions.ipynb | 92 +++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..4a4b27c 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,99 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "559c9293", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total unique products ordered: 1\n", + "Percentage of products ordered: 20.0 %\n", + "t-shirt 12\n", + "mug 11\n", + "hat 12\n", + "book 12\n", + "keychain 12\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory (products):\n", + "#I had the empty dictionary as a global variable, but it is not the best way to do it,\n", + "#it should be inside the function, locally!\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(\"Insert Quantity of: \" + product + \"s\"))\n", + " inventory[product] = quantity\n", + "#I had the return inside the loop. Return immediately stops the function, so that means that \n", + "#if it is inside the loop, as soon as the user gives the first input, the code stops!\n", + " return inventory\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "def get_customer_orders():\n", + "#Once again, I had the set outside of the function\n", + " customer_orders = set()\n", + " ask_customer = \"yes\"\n", + " \n", + " while ask_customer == \"yes\":\n", + " order = input(\"Select a product from the products list: \")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Invalid product. Please choose one from the list.\")\n", + " ask_customer = input(\"Do you want to order another product? (yes/no): \").lower()\n", + " \n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "#two parameters but only return one value because \n", + "#customer_orders = instructions.\n", + "#inventory = the thing being updated.\n", + "#You return only the thing that changed, which is inventory.\n", + "def update_inventory(customer_orders, inventory):\n", + " for item in customer_orders:\n", + " inventory[item] -= 1\n", + "\n", + " return inventory\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " total_available_products = len(products)\n", + " percentage_ordered = (total_products_ordered / total_available_products) * 100\n", + "\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"Total unique products ordered:\", total_products_ordered)\n", + " print(\"Percentage of products ordered:\", percentage_ordered, \"%\")\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "def print_updated_inventory(inventory):\n", + " for product, quantity in inventory.items():\n", + " print(product, quantity)\n", + "\n", + "print_updated_inventory(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +149,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,