diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..d42b322 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,285 @@ "\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": 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 +335,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,