From 376bb1f6340f47e2c811e47fa1cd1034bf0a0269 Mon Sep 17 00:00:00 2001 From: davherdel Date: Fri, 8 Aug 2025 00:20:32 +0100 Subject: [PATCH 1/3] Added full inventory functions --- lab-python-functions Functions.py | 52 ++++++ lab-python-functions.ipynb | 296 +++++++++++++++++++++++++++++- 2 files changed, 346 insertions(+), 2 deletions(-) create mode 100644 lab-python-functions Functions.py diff --git a/lab-python-functions Functions.py b/lab-python-functions Functions.py new file mode 100644 index 0000000..7627e31 --- /dev/null +++ b/lab-python-functions Functions.py @@ -0,0 +1,52 @@ +# Functions + +### Initialize Inventory +def initialize_inventory(products): + inventory = {} + for product in products: + quantity = int(input(f"Enter initial quantity for {product}: ")) + inventory[product] = quantity + return inventory + +### 2. Get customer orders +def get_customer_orders(): + customer_orders = set() + while True: + product_name = input("Enter product name to order (or 'done' to finish): ").strip() + if product_name.lower() == 'done': + break + customer_orders.add(product_name) + return customer_orders + +### 3. Update inventory +def update_inventory(customer_orders, inventory): + for product in customer_orders: + if product in inventory and inventory[product] > 0: + inventory[product] -= 1 + elif product in inventory: + print(f"⚠ {product} is out of stock!") + else: + print(f"⚠ {product} does not exist in inventory!") + + +### 4. Calculate order statistics +def calculate_order_statistics(customer_orders, products): + total_products_ordered = len(customer_orders) + unique_products_ordered = len(customer_orders.intersection(products)) + percentage_unique = (unique_products_ordered / len(products)) * 100 if products else 0 + return total_products_ordered, percentage_unique + + +# 5. Print order statistics +def print_order_statistics(order_statistics): + total_products_ordered, percentage_unique = order_statistics + print("\n📊 Order Statistics:") + print(f"Total products ordered: {total_products_ordered}") + print(f"Percentage of unique products ordered: {percentage_unique:.2f}%") + + +# 6. Print updated inventory +def print_updated_inventory(inventory): + print("\n📦 Updated Inventory:") + for product, quantity in inventory.items(): + print(f"{product}: {quantity}") diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..351234b 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,303 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "1fb787e1", + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Set up inventory\n", + "def initialize_inventory(prod_list):\n", + " inv = {}\n", + " for p in prod_list:\n", + " # ask for quantity\n", + " qty = input(\"Enter initial quantity for \" + p + \": \")\n", + " try:\n", + " qty = int(qty)\n", + " except:\n", + " qty = 0 # if bad input just 0\n", + " inv[p] = qty\n", + " return inv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2d2b23d4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Apples': 2, 'Bananas': 3, 'Oranges': 4}\n" + ] + } + ], + "source": [ + "# Testing the function 1\n", + "test_products = ['Apples', 'Bananas', 'Oranges']\n", + "inventory = initialize_inventory(test_products)\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "63afc896", + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Get customer order(s)\n", + "def get_customer_orders():\n", + " orders = set()\n", + " while True:\n", + " prod = input(\"Type product name to order (or done to stop): \").strip()\n", + " if prod.lower() == 'done':\n", + " break\n", + " if prod != \"\":\n", + " orders.add(prod)\n", + " return orders" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c5849a5f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'péssego', 'anana', 'banana'}\n" + ] + } + ], + "source": [ + "# Testing the function 2\n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "5689c4f9", + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Change inventory based on orders\n", + "def update_inventory(orders, inv):\n", + " for item in orders:\n", + " if item in inv:\n", + " if inv[item] > 0:\n", + " inv[item] = inv[item] - 1\n", + " else:\n", + " print(\"out of stock:\", item)\n", + " else:\n", + " print(item, \"doesn't exist??\")" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "40f83100", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pineapple doesn't exist??\n", + "out of stock: Oranges\n", + "{'Apples': 1, 'Bananas': 1, 'Oranges': 0}\n" + ] + } + ], + "source": [ + "# Testing the function 3\n", + "inventory = {'Apples': 2, 'Bananas': 1, 'Oranges': 0} \n", + "orders = {'Apples', 'Oranges', 'Pineapple'}\n", + "update_inventory(orders, inventory)\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "6bc40b8f", + "metadata": {}, + "outputs": [], + "source": [ + "# 4. Calculate stats\n", + "def calculate_order_statistics(orders, prod_list):\n", + " total = len(orders)\n", + " uniq = len(orders.intersection(prod_list))\n", + " if len(prod_list) > 0:\n", + " perc = (uniq / len(prod_list)) * 100\n", + " else:\n", + " perc = 0\n", + " return total, perc" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "174e40d7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total orders: 3\n", + "Percentage of valid products: 66.67%\n" + ] + } + ], + "source": [ + "# Testing the function 4\n", + "orders = {'Apples', 'Bananas', 'Pineapple'}\n", + "products = ['Apples', 'Bananas', 'Oranges']\n", + "total, perc = calculate_order_statistics(orders, products)\n", + "print(f\"Total orders: {total}\")\n", + "print(f\"Percentage of valid products: {perc:.2f}%\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "6489943c", + "metadata": {}, + "outputs": [], + "source": [ + "# 5. Print stats\n", + "def print_order_statistics(stats):\n", + " total, perc = stats\n", + " print(\"\\nOrder stats:\")\n", + " print(\"Total products ordered:\", total)\n", + " print(\"Unique %:\", round(perc, 2), \"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "e91b1704", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order stats:\n", + "Total products ordered: 3\n", + "Unique %: 66.67 %\n" + ] + } + ], + "source": [ + "# Testing the function 5\n", + "stats = (3, 66.67)\n", + "print_order_statistics(stats)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c3cb6f11", + "metadata": {}, + "outputs": [], + "source": [ + "# 6. Print final inventory\n", + "def print_updated_inventory(inv):\n", + " print(\"\\nInventory now:\")\n", + " for k, v in inv.items():\n", + " print(k, \":\", v)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "68a486a5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory now:\n", + "Apples : 4\n", + "Bananas : 2\n", + "Oranges : 0\n" + ] + } + ], + "source": [ + "# Testing the function 6\n", + "inventory = {'Apples': 4, 'Bananas': 2, 'Oranges': 0}\n", + "print_updated_inventory(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9fabd24c", + "metadata": {}, + "outputs": [], + "source": [ + "# 7. Main to run all above\n", + "def main():\n", + " prods = [\"apple\", \"banana\", \"orange\", \"mango\"]\n", + " inv = initialize_inventory(prods)\n", + " cust_orders = get_customer_orders()\n", + " update_inventory(cust_orders, inv)\n", + " s = calculate_order_statistics(cust_orders, set(prods))\n", + " print_order_statistics(s)\n", + " print_updated_inventory(inv)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "350b7184", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Péssego doesn't exist??\n", + "Pineapple doesn't exist??\n", + "Melancia doesn't exist??\n", + "\n", + "Order stats:\n", + "Total products ordered: 3\n", + "Unique %: 0.0 %\n", + "\n", + "Inventory now:\n", + "apple : 23\n", + "banana : 432345\n", + "orange : 543212\n", + "mango : 12345\n" + ] + } + ], + "source": [ + "# Execute the main function\n", + "if __name__ == \"__main__\":\n", + " main()\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +353,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4, From 5fa50f0d510683d8743795f4684e732f388caa1e Mon Sep 17 00:00:00 2001 From: davherdel Date: Mon, 11 Aug 2025 22:50:19 +0100 Subject: [PATCH 2/3] Finished the exercises --- lab-python-functions | 1 + lab-python-functions Functions.py | 52 ------------------------------- lab-python-functions.ipynb | 18 ----------- 3 files changed, 1 insertion(+), 70 deletions(-) create mode 160000 lab-python-functions delete mode 100644 lab-python-functions Functions.py diff --git a/lab-python-functions b/lab-python-functions new file mode 160000 index 0000000..392006e --- /dev/null +++ b/lab-python-functions @@ -0,0 +1 @@ +Subproject commit 392006efb90f36c79bbb163e6668fccfbccc0404 diff --git a/lab-python-functions Functions.py b/lab-python-functions Functions.py deleted file mode 100644 index 7627e31..0000000 --- a/lab-python-functions Functions.py +++ /dev/null @@ -1,52 +0,0 @@ -# Functions - -### Initialize Inventory -def initialize_inventory(products): - inventory = {} - for product in products: - quantity = int(input(f"Enter initial quantity for {product}: ")) - inventory[product] = quantity - return inventory - -### 2. Get customer orders -def get_customer_orders(): - customer_orders = set() - while True: - product_name = input("Enter product name to order (or 'done' to finish): ").strip() - if product_name.lower() == 'done': - break - customer_orders.add(product_name) - return customer_orders - -### 3. Update inventory -def update_inventory(customer_orders, inventory): - for product in customer_orders: - if product in inventory and inventory[product] > 0: - inventory[product] -= 1 - elif product in inventory: - print(f"⚠ {product} is out of stock!") - else: - print(f"⚠ {product} does not exist in inventory!") - - -### 4. Calculate order statistics -def calculate_order_statistics(customer_orders, products): - total_products_ordered = len(customer_orders) - unique_products_ordered = len(customer_orders.intersection(products)) - percentage_unique = (unique_products_ordered / len(products)) * 100 if products else 0 - return total_products_ordered, percentage_unique - - -# 5. Print order statistics -def print_order_statistics(order_statistics): - total_products_ordered, percentage_unique = order_statistics - print("\n📊 Order Statistics:") - print(f"Total products ordered: {total_products_ordered}") - print(f"Percentage of unique products ordered: {percentage_unique:.2f}%") - - -# 6. Print updated inventory -def print_updated_inventory(inventory): - print("\n📦 Updated Inventory:") - for product, quantity in inventory.items(): - print(f"{product}: {quantity}") diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 351234b..d42b322 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -286,24 +286,6 @@ "print_updated_inventory(inventory)\n" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "9fabd24c", - "metadata": {}, - "outputs": [], - "source": [ - "# 7. Main to run all above\n", - "def main():\n", - " prods = [\"apple\", \"banana\", \"orange\", \"mango\"]\n", - " inv = initialize_inventory(prods)\n", - " cust_orders = get_customer_orders()\n", - " update_inventory(cust_orders, inv)\n", - " s = calculate_order_statistics(cust_orders, set(prods))\n", - " print_order_statistics(s)\n", - " print_updated_inventory(inv)" - ] - }, { "cell_type": "code", "execution_count": 33, From a4a3d73fef0f6ce400973601cbdf8bbb288a9656 Mon Sep 17 00:00:00 2001 From: davherdel Date: Sat, 16 Aug 2025 15:14:18 +0100 Subject: [PATCH 3/3] Uploaded notebook --- lab-python-functions | 1 - 1 file changed, 1 deletion(-) delete mode 160000 lab-python-functions diff --git a/lab-python-functions b/lab-python-functions deleted file mode 160000 index 392006e..0000000 --- a/lab-python-functions +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 392006efb90f36c79bbb163e6668fccfbccc0404