From 62c7094fd7a3172333d6c01023b7383671b3ac31 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Wed, 22 Oct 2025 17:10:43 +0200 Subject: [PATCH 1/9] Update lab-python-functions.ipynb --- lab-python-functions.ipynb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..1bd6ad2 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,20 @@ "\n", "\n" ] + }, + { + "cell_type": "markdown", + "id": "d6bf0c1e", + "metadata": {}, + "source": [ + "ANSWER HERE" + ] + }, + { + "cell_type": "markdown", + "id": "d920eac0", + "metadata": {}, + "source": [] } ], "metadata": { From 288164ee5922e4845e2dd90160fd097cc97a9e17 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Wed, 22 Oct 2025 17:11:17 +0200 Subject: [PATCH 2/9] Update lab-python-functions.ipynb --- lab-python-functions.ipynb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 1bd6ad2..a60ccfb 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -56,6 +56,14 @@ "cell_type": "markdown", "id": "d920eac0", "metadata": {}, + "source": [ + "Another change" + ] + }, + { + "cell_type": "markdown", + "id": "f94a68d8", + "metadata": {}, "source": [] } ], From 3d8a1d98e96b6138e59bbf937bced0d19eba8045 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Thu, 23 Oct 2025 18:34:07 +0200 Subject: [PATCH 3/9] Update lab-python-functions.ipynb --- lab-python-functions.ipynb | 143 +++++++++++++++++++++++++++++++++++-- 1 file changed, 136 insertions(+), 7 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index a60ccfb..9cfda2e 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -49,7 +49,7 @@ "id": "d6bf0c1e", "metadata": {}, "source": [ - "ANSWER HERE" + "# Lab | Functions | Samia" ] }, { @@ -57,19 +57,148 @@ "id": "d920eac0", "metadata": {}, "source": [ - "Another change" + "## Answer:" ] }, { - "cell_type": "markdown", - "id": "f94a68d8", + "cell_type": "code", + "execution_count": null, + "id": "eb90bdaa", + "metadata": {}, + "outputs": [], + "source": [ + "#1. definition Function : initialize_inventory(products)\n", + "# Implement for initializing the inventory dictionary using a loop and user input\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for item in products:\n", + " quantity = int(input(f\"Enter quantity for {item}: \"))\n", + " inventory[item] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3384e992", + "metadata": {}, + "outputs": [], + "source": [ + "#2. definition Function : get_customer_orders()\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True: # Prompt the user to enter the product names using a loop\n", + " product = input(\"Enter a product name to order (or type 'done' to finish): \")\n", + " if product.lower() == 'done':\n", + " break\n", + " customer_orders.add(product)\n", + " return customer_orders # return Function : `customer_orders` set" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "233c68ec", + "metadata": {}, + "outputs": [], + "source": [ + "#3. definition Function : update_inventory(customer_orders, inventory)\n", + "def update_inventory(customer_orders, inventory):\n", + " for item in customer_orders: \n", + " if item in inventory: # Update the inventory based on customer orders\n", + " inventory[item] -= 1 # Decrease the quantity by 1\n", + " if inventory[item] == 0: # If the quantity reaches zero, remove the item from the inventory\n", + " del inventory[item]\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e9352bc1", "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "#4. definition Function : calculate_order_statistics(customer_orders, products)\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " unique_products_ordered = len(customer_orders.intersection(set(products)))\n", + "# Calculate order statistics (total products ordered, and percentage of unique products ordered)\n", + " order_statistics = {\n", + " \"total_products_ordered\": total_products_ordered,\n", + " \"percentage_unique_products_ordered\": (unique_products_ordered / len(products)) * 100 if products else 0\n", + " }\n", + " return order_statistics\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c360caba", + "metadata": {}, + "outputs": [], + "source": [ + "#5. definition Function : print_order_statistics(order_statistics)\n", + "def print_order_statistics(order_statistics):\n", + " for item, quantity in order_statistics.items(): # Print order statistics\n", + " print(f\"Product: {item}, Quantity Remaining: {quantity}\")\n", + "# Print order statistics\n", + " for item, quantity in order_statistics.items():\n", + " print(f\"Product: {item}, Quantity Remaining: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15431b0d", + "metadata": {}, + "outputs": [], + "source": [ + "#6. definition Function : print_updated_inventory(inventory)\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for item, quantity in inventory.items():\n", + " print(f\"Product: {item}, Quantity: {quantity}\")\n", + "# Print updated inventory\n", + " print(\"Updated Inventory:\")\n", + " for item, quantity in inventory.items():\n", + " print(f\"Product: {item}, Quantity: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9c570942", + "metadata": {}, + "outputs": [], + "source": [ + "#7. call all functions above in the appropriate sequence to execute the program and manage customer orders:\n", + "# List of products available from the previ labs\n", + "products = [\"apple\", \"banana\", \"orange\", \"grape\", \"mango\"]\n", + "\n", + "#1. call Function : initialize_inventory(products)\n", + "inventory = initialize_inventory(products)\n", + "\n", + "#2. call Function : get_customer_orders()\n", + "customer_orders = get_customer_orders()\n", + "\n", + "#3. call Function : update_inventory(customer_orders, inventory)\n", + "update_inventory(customer_orders, inventory)\n", + "\n", + "#6. call Function : print_updated_inventory(inventory)\n", + "print_updated_inventory(inventory)\n", + "\n", + "#4. call Function : calculate_order_statistics(customer_orders, products)\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "#5. call Function : print_order_statistics(order_statistics)\n", + "print_order_statistics(order_statistics)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -83,7 +212,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From 9160f14324a7751bbf75512f580b94b2dde5656c Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Thu, 23 Oct 2025 18:40:27 +0200 Subject: [PATCH 4/9] Update lab-python-functions.ipynb --- lab-python-functions.ipynb | 50 +++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 9cfda2e..7317c26 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -5,7 +5,7 @@ "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", "metadata": {}, "source": [ - "# Lab | Functions" + "# Lab | Functions | Samia" ] }, { @@ -62,7 +62,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "eb90bdaa", "metadata": {}, "outputs": [], @@ -88,7 +88,7 @@ "def get_customer_orders():\n", " customer_orders = set()\n", " while True: # Prompt the user to enter the product names using a loop\n", - " product = input(\"Enter a product name to order (or type 'done' to finish): \")\n", + " product = input(f\"Enter a product name to order (or type 'DONE' to finish your order): \")\n", " if product.lower() == 'done':\n", " break\n", " customer_orders.add(product)\n", @@ -97,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "233c68ec", "metadata": {}, "outputs": [], @@ -114,7 +114,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "e9352bc1", "metadata": {}, "outputs": [], @@ -133,7 +133,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "c360caba", "metadata": {}, "outputs": [], @@ -156,26 +156,54 @@ "source": [ "#6. definition Function : print_updated_inventory(inventory)\n", "def print_updated_inventory(inventory):\n", - " print(\"Updated Inventory:\")\n", + " print(f\"Updated Inventory:\")\n", " for item, quantity in inventory.items():\n", " print(f\"Product: {item}, Quantity: {quantity}\")\n", "# Print updated inventory\n", - " print(\"Updated Inventory:\")\n", + " print(f\"Updated Inventory:\")\n", " for item, quantity in inventory.items():\n", " print(f\"Product: {item}, Quantity: {quantity}\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "9c570942", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Updated Inventory:\n", + "Product: t-shirt, Quantity: 5\n", + "Product: mug, Quantity: 9\n", + "Product: hat, Quantity: 14\n", + "Product: book, Quantity: 19\n", + "Product: keychain, Quantity: 25\n", + "Updated Inventory:\n", + "Product: t-shirt, Quantity: 5\n", + "Product: mug, Quantity: 9\n", + "Product: hat, Quantity: 14\n", + "Product: book, Quantity: 19\n", + "Product: keychain, Quantity: 25\n", + "Product: total_products_ordered, Quantity Remaining: 3\n", + "Product: percentage_unique_products_ordered, Quantity Remaining: 60.0\n", + "Product: total_products_ordered, Quantity Remaining: 3\n", + "Product: percentage_unique_products_ordered, Quantity Remaining: 60.0\n" + ] + } + ], "source": [ "#7. call all functions above in the appropriate sequence to execute the program and manage customer orders:\n", "# List of products available from the previ labs\n", - "products = [\"apple\", \"banana\", \"orange\", \"grape\", \"mango\"]\n", "\n", + "# definition List: products \n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)\n", + "\n", + "# call Functions order:\n", "#1. call Function : initialize_inventory(products)\n", "inventory = initialize_inventory(products)\n", "\n", From cffc45b72b1e9bf4c6873acea4c70460b35baac0 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Mon, 3 Nov 2025 23:48:52 +0100 Subject: [PATCH 5/9] commit --- lab-python-functions.ipynb | 193 ++++++++++++++++++++++++++----------- 1 file changed, 137 insertions(+), 56 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7317c26..724d1a7 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -60,16 +60,24 @@ "## Answer:" ] }, + { + "cell_type": "markdown", + "id": "91749d13", + "metadata": {}, + "source": [ + "1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input." + ] + }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 81, "id": "eb90bdaa", "metadata": {}, "outputs": [], "source": [ "#1. definition Function : initialize_inventory(products)\n", "# Implement for initializing the inventory dictionary using a loop and user input\n", - "def initialize_inventory(products):\n", + "def initialize_inventory(products: list[str]):\n", " inventory = {}\n", " for item in products:\n", " quantity = int(input(f\"Enter quantity for {item}: \"))\n", @@ -77,9 +85,18 @@ " return inventory" ] }, + { + "cell_type": "markdown", + "id": "ab98b5bd", + "metadata": {}, + "source": [ + "\n", + "2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set." + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 88, "id": "3384e992", "metadata": {}, "outputs": [], @@ -87,34 +104,89 @@ "#2. definition Function : get_customer_orders()\n", "def get_customer_orders():\n", " customer_orders = set()\n", + " list_customer_orders = list(customer_orders)\n", " while True: # Prompt the user to enter the product names using a loop\n", - " product = input(f\"Enter a product name to order (or type 'DONE' to finish your order): \")\n", - " if product.lower() == 'done':\n", + " product = input(f\"Enter a product name to order (or type 'PURCHASE' to finish your order): \")\n", + " if product.lower() == 'purchase':\n", " break\n", - " customer_orders.add(product)\n", - " return customer_orders # return Function : `customer_orders` set" + " list_customer_orders.append(product)\n", + " return list_customer_orders # return Function : `customer_orders` set" + ] + }, + { + "cell_type": "markdown", + "id": "415cf4c0", + "metadata": {}, + "source": [ + "\n", + "3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders." ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "233c68ec", "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "expected ':' (3909888478.py, line 11)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[89], line 11\u001b[1;36m\u001b[0m\n\u001b[1;33m else item not in inventory: # If the item is not found in the inventory, print a message\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m expected ':'\n" + ] + } + ], + "source": [ + "#3. definition Function : update_inventory(customer_orders, inventory)\n", + "def update_inventory(customer_orders, inventory):\n", + " list_customer_orders = list(customer_orders) # store set as a list for processing\n", + " while True:\n", + " for item in list_customer_orders: # Use the list for iteration\n", + " \n", + " if item in inventory and inventory[item] > 0: # Update the inventory based on customer orders\n", + " inventory[item] -= 1 # Decrease the quantity by 1\n", + " elif item in inventory and inventory[item] == 0: # If the quantity reaches zero, remove the item from the inventory\n", + " print(f\"Item '{item}' is out of stock and has been removed from the inventory.\")\n", + " else: # If the item is not found in the inventory, print a message\n", + " print(f\"Item '{item}' is not available in the inventory.\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "5d891892", + "metadata": {}, "outputs": [], "source": [ "#3. definition Function : update_inventory(customer_orders, inventory)\n", "def update_inventory(customer_orders, inventory):\n", - " for item in customer_orders: \n", - " if item in inventory: # Update the inventory based on customer orders\n", - " inventory[item] -= 1 # Decrease the quantity by 1\n", - " if inventory[item] == 0: # If the quantity reaches zero, remove the item from the inventory\n", - " del inventory[item]\n", + " list_customer_orders = list(customer_orders) # store set as a list for processing\n", + "\n", + " for item in list_customer_orders: # Use the list for iteration\n", + " if item in inventory and inventory[item] > 0: # Update the inventory based on customer orders\n", + " inventory[item] -= 1 # Decrease the quantity by 1\n", + " elif item in inventory and inventory[item] == 0: # If the quantity reaches zero, remove the item from the inventory\n", + " print(f\"Item '{item}' is out of stock and has been removed from the inventory.\")\n", + " else: # If the item is not found in the inventory, print a message\n", + " print(f\"Item '{item}' is not available in the inventory.\")\n", " return inventory" ] }, + { + "cell_type": "markdown", + "id": "65d44dad", + "metadata": {}, + "source": [ + "\n", + "4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values." + ] + }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 91, "id": "e9352bc1", "metadata": {}, "outputs": [], @@ -131,9 +203,18 @@ " return order_statistics\n" ] }, + { + "cell_type": "markdown", + "id": "77c72854", + "metadata": {}, + "source": [ + "\n", + "5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics." + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 92, "id": "c360caba", "metadata": {}, "outputs": [], @@ -141,87 +222,87 @@ "#5. definition Function : print_order_statistics(order_statistics)\n", "def print_order_statistics(order_statistics):\n", " for item, quantity in order_statistics.items(): # Print order statistics\n", - " print(f\"Product: {item}, Quantity Remaining: {quantity}\")\n", - "# Print order statistics\n", - " for item, quantity in order_statistics.items():\n", - " print(f\"Product: {item}, Quantity Remaining: {quantity}\")" + " print(f\"Product: {item}, Quantity Remaining: {quantity},\\n\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 93, "id": "15431b0d", "metadata": {}, "outputs": [], "source": [ "#6. definition Function : print_updated_inventory(inventory)\n", "def print_updated_inventory(inventory):\n", - " print(f\"Updated Inventory:\")\n", - " for item, quantity in inventory.items():\n", - " print(f\"Product: {item}, Quantity: {quantity}\")\n", - "# Print updated inventory\n", - " print(f\"Updated Inventory:\")\n", - " for item, quantity in inventory.items():\n", - " print(f\"Product: {item}, Quantity: {quantity}\")" + " print(f\"Updated Inventory:\")\n", + " for item, quantity in inventory.items():\n", + " print(f\"Product: {item}, Quantity: {quantity}\")" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "9c570942", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "Updated Inventory:\n", - "Product: t-shirt, Quantity: 5\n", - "Product: mug, Quantity: 9\n", - "Product: hat, Quantity: 14\n", - "Product: book, Quantity: 19\n", - "Product: keychain, Quantity: 25\n", - "Updated Inventory:\n", - "Product: t-shirt, Quantity: 5\n", - "Product: mug, Quantity: 9\n", - "Product: hat, Quantity: 14\n", - "Product: book, Quantity: 19\n", - "Product: keychain, Quantity: 25\n", - "Product: total_products_ordered, Quantity Remaining: 3\n", - "Product: percentage_unique_products_ordered, Quantity Remaining: 60.0\n", - "Product: total_products_ordered, Quantity Remaining: 3\n", - "Product: percentage_unique_products_ordered, Quantity Remaining: 60.0\n" + "ename": "SyntaxError", + "evalue": "invalid syntax (422362016.py, line 4)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[99], line 4\u001b[1;36m\u001b[0m\n\u001b[1;33m from lab-python-lists import products\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "#7. call all functions above in the appropriate sequence to execute the program and manage customer orders:\n", "# List of products available from the previ labs\n", + "from sys import exception\n", + "\n", "\n", + "try :\n", + " from lab-python-lists import products\n", "# definition List: products \n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", - "print(products)\n", + "print('List of products:', products)\n", "\n", "# call Functions order:\n", "#1. call Function : initialize_inventory(products)\n", "inventory = initialize_inventory(products)\n", "\n", "#2. call Function : get_customer_orders()\n", - "customer_orders = get_customer_orders()\n", + "list_customer_orders = get_customer_orders()\n", "\n", "#3. call Function : update_inventory(customer_orders, inventory)\n", - "update_inventory(customer_orders, inventory)\n", - "\n", - "#6. call Function : print_updated_inventory(inventory)\n", - "print_updated_inventory(inventory)\n", + "update_inventory(list_customer_orders, inventory)\n", "\n", "#4. call Function : calculate_order_statistics(customer_orders, products)\n", - "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "order_statistics = calculate_order_statistics(list_customer_orders, products)\n", "\n", "#5. call Function : print_order_statistics(order_statistics)\n", - "print_order_statistics(order_statistics)" + "print_order_statistics(order_statistics)\n", + "\n", + "#6. call Function : print_updated_inventory(inventory)\n", + "print_updated_inventory(inventory)\n", + "exception ex as e:\n", + " print(\"An error occurred during the execution of the program. Please check your inputs and try again.\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c4a3307", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0dc43b6e", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { From 73661192cc4b2050e0972c7e9a20be294c35927f Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Tue, 4 Nov 2025 02:23:03 +0100 Subject: [PATCH 6/9] almost done need to revise tuples --- lab-python-functions.ipynb | 210 ++++++++++++++++++++----------------- 1 file changed, 111 insertions(+), 99 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 724d1a7..4590184 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -5,7 +5,8 @@ "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", "metadata": {}, "source": [ - "# Lab | Functions | Samia" + "# Lab 03 | Functions\n", + "-------" ] }, { @@ -41,7 +42,7 @@ "- Utilize function parameters and return values to transfer data between functions.\n", "- Test your functions individually to ensure they work correctly.\n", "\n", - "\n" + "------\n" ] }, { @@ -49,15 +50,19 @@ "id": "d6bf0c1e", "metadata": {}, "source": [ - "# Lab | Functions | Samia" + "# Solved Lab 03 | Functions\n", + "------" ] }, { - "cell_type": "markdown", - "id": "d920eac0", + "cell_type": "code", + "execution_count": 22, + "id": "9972bdb3", "metadata": {}, + "outputs": [], "source": [ - "## Answer:" + "#0. List of products from the previous lab\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" ] }, { @@ -70,18 +75,19 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 23, "id": "eb90bdaa", "metadata": {}, "outputs": [], "source": [ - "#1. definition Function : initialize_inventory(products)\n", + "#1. def Function : initialize_inventory(products)\n", "# Implement for initializing the inventory dictionary using a loop and user input\n", "def initialize_inventory(products: list[str]):\n", " inventory = {}\n", " for item in products:\n", " quantity = int(input(f\"Enter quantity for {item}: \"))\n", - " inventory[item] = quantity\n", + " inventory[item] = quantity # key is item, value is quantity \n", + "\n", " return inventory" ] }, @@ -96,21 +102,27 @@ }, { "cell_type": "code", - "execution_count": 88, - "id": "3384e992", + "execution_count": 24, + "id": "5dc91527", "metadata": {}, "outputs": [], "source": [ - "#2. definition Function : get_customer_orders()\n", "def get_customer_orders():\n", + " products = {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n", " customer_orders = set()\n", - " list_customer_orders = list(customer_orders)\n", - " while True: # Prompt the user to enter the product names using a loop\n", - " product = input(f\"Enter a product name to order (or type 'PURCHASE' to finish your order): \")\n", - " if product.lower() == 'purchase':\n", - " break\n", - " list_customer_orders.append(product)\n", - " return list_customer_orders # return Function : `customer_orders` set" + "\n", + " while True: # Prompt the user to enter product names using a loop\n", + " product = input(\"Enter a product name to order (or type 'PURCHASE' to finish): \").lower()\n", + " \n", + " if product == \"purchase\":\n", + " print(\"Your purchase list (as a set):\", customer_orders)\n", + " return customer_orders # ✅ returns a set\n", + " \n", + " if product in products:\n", + " customer_orders.add(product)\n", + " print(f\"{product} added to your order list.\")\n", + " else:\n", + " print(f\"'{product}' is not available.\")" ] }, { @@ -124,55 +136,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "233c68ec", "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "expected ':' (3909888478.py, line 11)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m Cell \u001b[1;32mIn[89], line 11\u001b[1;36m\u001b[0m\n\u001b[1;33m else item not in inventory: # If the item is not found in the inventory, print a message\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m expected ':'\n" - ] - } - ], + "outputs": [], "source": [ - "#3. definition Function : update_inventory(customer_orders, inventory)\n", - "def update_inventory(customer_orders, inventory):\n", + "#3. def Function : update_inventory(customer_orders, inventory)\n", + "def update_inventory(customer_orders: set, inventory: dict):\n", " list_customer_orders = list(customer_orders) # store set as a list for processing\n", " while True:\n", " for item in list_customer_orders: # Use the list for iteration\n", - " \n", - " if item in inventory and inventory[item] > 0: # Update the inventory based on customer orders\n", + " # Update the inventory based on customer orders\n", + " if item in inventory and inventory[item] > 0: \n", " inventory[item] -= 1 # Decrease the quantity by 1\n", - " elif item in inventory and inventory[item] == 0: # If the quantity reaches zero, remove the item from the inventory\n", - " print(f\"Item '{item}' is out of stock and has been removed from the inventory.\")\n", - " else: # If the item is not found in the inventory, print a message\n", - " print(f\"Item '{item}' is not available in the inventory.\")\n", - "\n", - " return inventory" - ] - }, - { - "cell_type": "code", - "execution_count": 90, - "id": "5d891892", - "metadata": {}, - "outputs": [], - "source": [ - "#3. definition Function : update_inventory(customer_orders, inventory)\n", - "def update_inventory(customer_orders, inventory):\n", - " list_customer_orders = list(customer_orders) # store set as a list for processing\n", - "\n", - " for item in list_customer_orders: # Use the list for iteration\n", - " if item in inventory and inventory[item] > 0: # Update the inventory based on customer orders\n", - " inventory[item] -= 1 # Decrease the quantity by 1\n", - " elif item in inventory and inventory[item] == 0: # If the quantity reaches zero, remove the item from the inventory\n", - " print(f\"Item '{item}' is out of stock and has been removed from the inventory.\")\n", - " else: # If the item is not found in the inventory, print a message\n", - " print(f\"Item '{item}' is not available in the inventory.\")\n", - " return inventory" + " # If the quantity reaches zero, remove the item from the inventory\n", + " elif item in inventory and inventory[item] == 0: \n", + " print(f\"{item} is out of stock and has been removed from the inventory.\")\n", + " # If the item is not found in the inventory, print a message\n", + " else: \n", + " print(f\"{item} is not available in the inventory.\")\n", + " return inventory" ] }, { @@ -186,21 +169,29 @@ }, { "cell_type": "code", - "execution_count": 91, - "id": "e9352bc1", + "execution_count": 26, + "id": "96dc59c8", "metadata": {}, "outputs": [], "source": [ - "#4. definition Function : calculate_order_statistics(customer_orders, products)\n", + "#4. def Function : calculate_order_statistics(customer_orders, products)\n", + "\n", "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " # Total products ordered\n", " total_products_ordered = len(customer_orders)\n", - " unique_products_ordered = len(customer_orders.intersection(set(products)))\n", - "# Calculate order statistics (total products ordered, and percentage of unique products ordered)\n", + "\n", + " # Unique products ordered that exist in the product list\n", + " unique_products_ordered = len(customer_orders.intersection(products))\n", + "\n", + " # Percentage calculation (avoid division by zero)\n", + " percentage_unique = (unique_products_ordered / len(products) * 100) if products else 0\n", + " \n", " order_statistics = {\n", " \"total_products_ordered\": total_products_ordered,\n", - " \"percentage_unique_products_ordered\": (unique_products_ordered / len(products)) * 100 if products else 0\n", + " \"percentage_unique_products_ordered\": percentage_unique,\n", " }\n", - " return order_statistics\n" + " return total_products_ordered, percentage_unique\n" ] }, { @@ -214,55 +205,85 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 27, "id": "c360caba", "metadata": {}, "outputs": [], "source": [ - "#5. definition Function : print_order_statistics(order_statistics)\n", + "#5. def Function : print_order_statistics(order_statistics)\n", "def print_order_statistics(order_statistics):\n", - " for item, quantity in order_statistics.items(): # Print order statistics\n", - " print(f\"Product: {item}, Quantity Remaining: {quantity},\\n\")" + " for product, quantity in order_statistics.items(): # Print order statistics\n", + " print(f\"Product: {product}, Quantity Remaining: {quantity},\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "1d00fd4f", + "metadata": {}, + "source": [ + "6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory." ] }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 28, "id": "15431b0d", "metadata": {}, "outputs": [], "source": [ - "#6. definition Function : print_updated_inventory(inventory)\n", - "def print_updated_inventory(inventory):\n", + "#6. def Function : print_updated_inventory(inventory)\n", + "def print_updated_inventory(inventory:dict):\n", " print(f\"Updated Inventory:\")\n", - " for item, quantity in inventory.items():\n", - " print(f\"Product: {item}, Quantity: {quantity}\")" + " for product, quantity in inventory.items():\n", + " print(f\"Product: {product}, Quantity: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "id": "0b05f70a", + "metadata": {}, + "source": [ + "7. Call the functions in the appropriate sequence to execute the program and manage customer orders." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "9c570942", "metadata": {}, "outputs": [ { - "ename": "SyntaxError", - "evalue": "invalid syntax (422362016.py, line 4)", + "name": "stdout", + "output_type": "stream", + "text": [ + "List of products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "'tshirt' is not available.\n", + "t-shirt added to your order list.\n", + "mug added to your order list.\n", + "hat added to your order list.\n", + "book added to your order list.\n", + "Your purchase list (as a set): {'t-shirt', 'book', 'hat', 'mug'}\n", + "'' is not available.\n", + "Your purchase list (as a set): set()\n" + ] + }, + { + "ename": "AttributeError", + "evalue": "'tuple' object has no attribute 'items'", "output_type": "error", "traceback": [ - "\u001b[1;36m Cell \u001b[1;32mIn[99], line 4\u001b[1;36m\u001b[0m\n\u001b[1;33m from lab-python-lists import products\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[29], line 22\u001b[0m\n\u001b[0;32m 19\u001b[0m order_statistics \u001b[38;5;241m=\u001b[39m calculate_order_statistics(list_customer_orders, products)\n\u001b[0;32m 21\u001b[0m \u001b[38;5;66;03m#5. call Function : print_order_statistics(order_statistics)\u001b[39;00m\n\u001b[1;32m---> 22\u001b[0m print_order_statistics(order_statistics)\n\u001b[0;32m 24\u001b[0m \u001b[38;5;66;03m#6. call Function : print_updated_inventory(inventory)\u001b[39;00m\n\u001b[0;32m 25\u001b[0m print_updated_inventory(inventory)\n", + "Cell \u001b[1;32mIn[27], line 3\u001b[0m, in \u001b[0;36mprint_order_statistics\u001b[1;34m(order_statistics)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mprint_order_statistics\u001b[39m(order_statistics):\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m product, quantity \u001b[38;5;129;01min\u001b[39;00m order_statistics\u001b[38;5;241m.\u001b[39mitems(): \u001b[38;5;66;03m# Print order statistics\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mProduct: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mproduct\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, Quantity Remaining: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquantity\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m,\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'items'" ] } ], "source": [ "#7. call all functions above in the appropriate sequence to execute the program and manage customer orders:\n", - "# List of products available from the previ labs\n", - "from sys import exception\n", - "\n", "\n", - "try :\n", - " from lab-python-lists import products\n", - "# definition List: products \n", + "# def List: products \n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "print('List of products:', products)\n", "\n", @@ -271,6 +292,7 @@ "inventory = initialize_inventory(products)\n", "\n", "#2. call Function : get_customer_orders()\n", + "get_customer_orders()\n", "list_customer_orders = get_customer_orders()\n", "\n", "#3. call Function : update_inventory(customer_orders, inventory)\n", @@ -283,23 +305,13 @@ "print_order_statistics(order_statistics)\n", "\n", "#6. call Function : print_updated_inventory(inventory)\n", - "print_updated_inventory(inventory)\n", - "exception ex as e:\n", - " print(\"An error occurred during the execution of the program. Please check your inputs and try again.\")" + "print_updated_inventory(inventory)" ] }, { "cell_type": "code", "execution_count": null, - "id": "2c4a3307", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0dc43b6e", + "id": "b13dd544", "metadata": {}, "outputs": [], "source": [] From a8a816c6880c32ef9a31a2eabb27656464b5a9b4 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Tue, 4 Nov 2025 18:12:40 +0100 Subject: [PATCH 7/9] commit --- lab-python-functions.ipynb | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 4590184..a12c53f 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 40, "id": "9972bdb3", "metadata": {}, "outputs": [], @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 41, "id": "eb90bdaa", "metadata": {}, "outputs": [], @@ -102,7 +102,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 42, "id": "5dc91527", "metadata": {}, "outputs": [], @@ -136,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 43, "id": "233c68ec", "metadata": {}, "outputs": [], @@ -169,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 44, "id": "96dc59c8", "metadata": {}, "outputs": [], @@ -205,15 +205,21 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 45, "id": "c360caba", "metadata": {}, "outputs": [], "source": [ "#5. def Function : print_order_statistics(order_statistics)\n", + "\n", "def print_order_statistics(order_statistics):\n", - " for product, quantity in order_statistics.items(): # Print order statistics\n", - " print(f\"Product: {product}, Quantity Remaining: {quantity},\\n\")" + "\n", + " # Extract the dictionary if wrapped in a tuple\n", + " if isinstance(order_statistics, tuple) and len(order_statistics) > 0:\n", + " order_statistics = order_statistics[0] \n", + " # Print order statistics\n", + " for product, quantity in order_statistics.items():\n", + " print(f\"Product: {product}, Quantity Remaining: {quantity}\\n\") " ] }, { @@ -226,7 +232,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 46, "id": "15431b0d", "metadata": {}, "outputs": [], @@ -248,7 +254,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 47, "id": "9c570942", "metadata": {}, "outputs": [ @@ -259,24 +265,23 @@ "List of products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", "'tshirt' is not available.\n", "t-shirt added to your order list.\n", - "mug added to your order list.\n", "hat added to your order list.\n", "book added to your order list.\n", + "mug added to your order list.\n", "Your purchase list (as a set): {'t-shirt', 'book', 'hat', 'mug'}\n", - "'' is not available.\n", "Your purchase list (as a set): set()\n" ] }, { "ename": "AttributeError", - "evalue": "'tuple' object has no attribute 'items'", + "evalue": "'int' object has no attribute 'items'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[29], line 22\u001b[0m\n\u001b[0;32m 19\u001b[0m order_statistics \u001b[38;5;241m=\u001b[39m calculate_order_statistics(list_customer_orders, products)\n\u001b[0;32m 21\u001b[0m \u001b[38;5;66;03m#5. call Function : print_order_statistics(order_statistics)\u001b[39;00m\n\u001b[1;32m---> 22\u001b[0m print_order_statistics(order_statistics)\n\u001b[0;32m 24\u001b[0m \u001b[38;5;66;03m#6. call Function : print_updated_inventory(inventory)\u001b[39;00m\n\u001b[0;32m 25\u001b[0m print_updated_inventory(inventory)\n", - "Cell \u001b[1;32mIn[27], line 3\u001b[0m, in \u001b[0;36mprint_order_statistics\u001b[1;34m(order_statistics)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mprint_order_statistics\u001b[39m(order_statistics):\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m product, quantity \u001b[38;5;129;01min\u001b[39;00m order_statistics\u001b[38;5;241m.\u001b[39mitems(): \u001b[38;5;66;03m# Print order statistics\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mProduct: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mproduct\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, Quantity Remaining: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquantity\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m,\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", - "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'items'" + "Cell \u001b[1;32mIn[47], line 22\u001b[0m\n\u001b[0;32m 19\u001b[0m order_statistics \u001b[38;5;241m=\u001b[39m calculate_order_statistics(list_customer_orders, products)\n\u001b[0;32m 21\u001b[0m \u001b[38;5;66;03m#5. call Function : print_order_statistics(order_statistics)\u001b[39;00m\n\u001b[1;32m---> 22\u001b[0m print_order_statistics(order_statistics)\n\u001b[0;32m 24\u001b[0m \u001b[38;5;66;03m#6. call Function : print_updated_inventory(inventory)\u001b[39;00m\n\u001b[0;32m 25\u001b[0m print_updated_inventory(inventory)\n", + "Cell \u001b[1;32mIn[45], line 9\u001b[0m, in \u001b[0;36mprint_order_statistics\u001b[1;34m(order_statistics)\u001b[0m\n\u001b[0;32m 7\u001b[0m order_statistics \u001b[38;5;241m=\u001b[39m order_statistics[\u001b[38;5;241m0\u001b[39m] \n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# Print order statistics\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m product, quantity \u001b[38;5;129;01min\u001b[39;00m order_statistics\u001b[38;5;241m.\u001b[39mitems():\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mProduct: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mproduct\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, Quantity Remaining: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquantity\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[1;31mAttributeError\u001b[0m: 'int' object has no attribute 'items'" ] } ], From 224ef9f2fc2bf44dccbe1a912aa8b629719ef213 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Wed, 5 Nov 2025 18:25:23 +0100 Subject: [PATCH 8/9] commit --- lab-python-functions.ipynb | 131 ++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 53 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index a12c53f..2990f22 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "id": "9972bdb3", "metadata": {}, "outputs": [], @@ -75,20 +75,49 @@ }, { "cell_type": "code", - "execution_count": 41, - "id": "eb90bdaa", + "execution_count": 83, + "id": "1ae5a04a", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "✅ Quantity = 10 for t-shirt\n", + "✅ Quantity = 20 for mug\n", + "✅ Quantity = 30 for hat\n", + "✅ Quantity = 40 for book\n", + "✅ Quantity = 50 for keychain\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#1. def Function : initialize_inventory(products)\n", - "# Implement for initializing the inventory dictionary using a loop and user input\n", - "def initialize_inventory(products: list[str]):\n", + "# Initialize inventory dict using a loop and user input\n", + "def initialize_inventory(products):\n", " inventory = {}\n", " for item in products:\n", - " quantity = int(input(f\"Enter quantity for {item}: \"))\n", - " inventory[item] = quantity # key is item, value is quantity \n", + " while True:\n", + " quantity = input(f\"Enter quantity for {item}: \")\n", + " if quantity.isdigit(): # Checks if the input contains only digits\n", + " inventory[item] = int(quantity) # key is item, value is quantity \n", + " print(f\"✅ Quantity = {inventory[item]} for {item}\")\n", + " break\n", + " else:\n", + " print(f\"❌ Invalid input! Please enter a number only for {item}\")\n", + " return inventory\n", "\n", - " return inventory" + "initialize_inventory(products)" ] }, { @@ -102,29 +131,52 @@ }, { "cell_type": "code", - "execution_count": 42, - "id": "5dc91527", + "execution_count": null, + "id": "6c2aa017", "metadata": {}, "outputs": [], "source": [ + "#2. def Function : get_customer_orders \n", "def get_customer_orders():\n", " products = {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n", " customer_orders = set()\n", "\n", - " while True: # Prompt the user to enter product names using a loop\n", - " product = input(\"Enter a product name to order (or type 'PURCHASE' to finish): \").lower()\n", + " product = \"\"\n", + " while product != \"purchase\":\n", + " product = input(\"Enter a product name to order (or type 'PURCHASE' to finish): \").strip().lower()\n", + "\n", + " # Empty input\n", + " if product == \"\":\n", + " print(\"⚠ You didn't type anything. Please enter a product name.\")\n", + " continue\n", " \n", + " # Finish purchase\n", " if product == \"purchase\":\n", - " print(\"Your purchase list (as a set):\", customer_orders)\n", - " return customer_orders # ✅ returns a set\n", - " \n", + " break\n", + "\n", + " # Valid product\n", " if product in products:\n", " customer_orders.add(product)\n", - " print(f\"{product} added to your order list.\")\n", + " print(f\"✅ '{product}' added to your order.\")\n", + "\n", + " # Invalid product\n", " else:\n", - " print(f\"'{product}' is not available.\")" + " print(f\"❌ '{product}' is not available.\")\n", + " print(\"Available products:\", \", \".join(products))\n", + "\n", + " print(\"\\n✅ Purchase complete!\")\n", + " print(\"Your purchase list:\", customer_orders)\n", + " return customer_orders" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "b5e8f596", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "415cf4c0", @@ -136,7 +188,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "id": "233c68ec", "metadata": {}, "outputs": [], @@ -169,7 +221,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "id": "96dc59c8", "metadata": {}, "outputs": [], @@ -205,7 +257,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "id": "c360caba", "metadata": {}, "outputs": [], @@ -232,7 +284,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "id": "15431b0d", "metadata": {}, "outputs": [], @@ -254,37 +306,10 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "id": "9c570942", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "List of products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "'tshirt' is not available.\n", - "t-shirt added to your order list.\n", - "hat added to your order list.\n", - "book added to your order list.\n", - "mug added to your order list.\n", - "Your purchase list (as a set): {'t-shirt', 'book', 'hat', 'mug'}\n", - "Your purchase list (as a set): set()\n" - ] - }, - { - "ename": "AttributeError", - "evalue": "'int' object has no attribute 'items'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[47], line 22\u001b[0m\n\u001b[0;32m 19\u001b[0m order_statistics \u001b[38;5;241m=\u001b[39m calculate_order_statistics(list_customer_orders, products)\n\u001b[0;32m 21\u001b[0m \u001b[38;5;66;03m#5. call Function : print_order_statistics(order_statistics)\u001b[39;00m\n\u001b[1;32m---> 22\u001b[0m print_order_statistics(order_statistics)\n\u001b[0;32m 24\u001b[0m \u001b[38;5;66;03m#6. call Function : print_updated_inventory(inventory)\u001b[39;00m\n\u001b[0;32m 25\u001b[0m print_updated_inventory(inventory)\n", - "Cell \u001b[1;32mIn[45], line 9\u001b[0m, in \u001b[0;36mprint_order_statistics\u001b[1;34m(order_statistics)\u001b[0m\n\u001b[0;32m 7\u001b[0m order_statistics \u001b[38;5;241m=\u001b[39m order_statistics[\u001b[38;5;241m0\u001b[39m] \n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# Print order statistics\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m product, quantity \u001b[38;5;129;01min\u001b[39;00m order_statistics\u001b[38;5;241m.\u001b[39mitems():\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mProduct: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mproduct\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, Quantity Remaining: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mquantity\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", - "\u001b[1;31mAttributeError\u001b[0m: 'int' object has no attribute 'items'" - ] - } - ], + "outputs": [], "source": [ "#7. call all functions above in the appropriate sequence to execute the program and manage customer orders:\n", "\n", @@ -307,7 +332,7 @@ "order_statistics = calculate_order_statistics(list_customer_orders, products)\n", "\n", "#5. call Function : print_order_statistics(order_statistics)\n", - "print_order_statistics(order_statistics)\n", + "#print_order_statistics(order_statistics)\n", "\n", "#6. call Function : print_updated_inventory(inventory)\n", "print_updated_inventory(inventory)" @@ -316,7 +341,7 @@ { "cell_type": "code", "execution_count": null, - "id": "b13dd544", + "id": "a378e516", "metadata": {}, "outputs": [], "source": [] From d2c629aba623390c06ec2a2f6341240b84690ed9 Mon Sep 17 00:00:00 2001 From: samia-boubaya Date: Wed, 5 Nov 2025 19:34:49 +0100 Subject: [PATCH 9/9] commit --- lab-python-functions.ipynb | 130 ++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 51 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 2990f22..58cdb08 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -75,32 +75,10 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": null, "id": "1ae5a04a", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "✅ Quantity = 10 for t-shirt\n", - "✅ Quantity = 20 for mug\n", - "✅ Quantity = 30 for hat\n", - "✅ Quantity = 40 for book\n", - "✅ Quantity = 50 for keychain\n" - ] - }, - { - "data": { - "text/plain": [ - "{'t-shirt': 10, 'mug': 20, 'hat': 30, 'book': 40, 'keychain': 50}" - ] - }, - "execution_count": 83, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "#1. def Function : initialize_inventory(products)\n", "# Initialize inventory dict using a loop and user input\n", @@ -114,10 +92,8 @@ " print(f\"✅ Quantity = {inventory[item]} for {item}\")\n", " break\n", " else:\n", - " print(f\"❌ Invalid input! Please enter a number only for {item}\")\n", - " return inventory\n", - "\n", - "initialize_inventory(products)" + " print(f\"⚠ Invalid input! Please enter a number only for {item}\")\n", + " return inventory" ] }, { @@ -162,21 +138,13 @@ " # Invalid product\n", " else:\n", " print(f\"❌ '{product}' is not available.\")\n", - " print(\"Available products:\", \", \".join(products))\n", + " print(f\"Available products: ,{products}\")\n", "\n", " print(\"\\n✅ Purchase complete!\")\n", " print(\"Your purchase list:\", customer_orders)\n", " return customer_orders" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "b5e8f596", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "415cf4c0", @@ -194,7 +162,7 @@ "outputs": [], "source": [ "#3. def Function : update_inventory(customer_orders, inventory)\n", - "def update_inventory(customer_orders: set, inventory: dict):\n", + "def update_inventory(customer_orders:set, inventory:dict):\n", " list_customer_orders = list(customer_orders) # store set as a list for processing\n", " while True:\n", " for item in list_customer_orders: # Use the list for iteration\n", @@ -228,7 +196,7 @@ "source": [ "#4. def Function : calculate_order_statistics(customer_orders, products)\n", "\n", - "def calculate_order_statistics(customer_orders, products):\n", + "def calculate_order_statistics(customer_orders:set, products:list):\n", "\n", " # Total products ordered\n", " total_products_ordered = len(customer_orders)\n", @@ -263,8 +231,11 @@ "outputs": [], "source": [ "#5. def Function : print_order_statistics(order_statistics)\n", + "# DICT\n", + "order_statistics = {}\n", + "order_statistics[\"total_products_ordered\"], order_statistics[\"percentage_unique_products_ordered\"] = calculate_order_statistics(get_customer_orders(), products)\n", "\n", - "def print_order_statistics(order_statistics):\n", + "def print_order_statistics(order_statistics:dict):\n", "\n", " # Extract the dictionary if wrapped in a tuple\n", " if isinstance(order_statistics, tuple) and len(order_statistics) > 0:\n", @@ -284,16 +255,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 105, "id": "15431b0d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "Product: t-shirt, Quantity: 10\n", + "Product: mug, Quantity: 20\n", + "Product: hat, Quantity: 30\n", + "Product: book, Quantity: 40\n", + "Product: keychain, Quantity: 50\n" + ] + } + ], "source": [ "#6. def Function : print_updated_inventory(inventory)\n", - "def print_updated_inventory(inventory:dict):\n", + "def print_updated_inventory(inventory):\n", " print(f\"Updated Inventory:\")\n", " for product, quantity in inventory.items():\n", - " print(f\"Product: {product}, Quantity: {quantity}\")" + " print(f\"Product: {product}, Quantity: {quantity}\")\n", + "\n", + "print_updated_inventory(inventory)" ] }, { @@ -306,10 +292,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 103, "id": "9c570942", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "List of products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "✅ Quantity = 10 for t-shirt\n", + "✅ Quantity = 20 for mug\n", + "✅ Quantity = 30 for hat\n", + "✅ Quantity = 40 for book\n", + "✅ Quantity = 60 for keychain\n", + "✅ 'keychain' added to your order.\n", + "❌ 'haty' is not available.\n", + "Available products: ,{'book', 't-shirt', 'keychain', 'mug', 'hat'}\n", + "✅ 'hat' added to your order.\n", + "✅ 'book' added to your order.\n", + "\n", + "✅ Purchase complete!\n", + "Your purchase list: {'keychain', 'hat', 'book'}\n", + "⚠ You didn't type anything. Please enter a product name.\n", + "⚠ You didn't type anything. Please enter a product name.\n", + "⚠ You didn't type anything. Please enter a product name.\n", + "⚠ You didn't type anything. Please enter a product name.\n", + "⚠ You didn't type anything. Please enter a product name.\n", + "⚠ You didn't type anything. Please enter a product name.\n", + "⚠ You didn't type anything. Please enter a product name.\n" + ] + }, + { + "ename": "KeyboardInterrupt", + "evalue": "Interrupted by user", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[103], line 16\u001b[0m\n\u001b[0;32m 12\u001b[0m get_customer_orders()\n\u001b[0;32m 15\u001b[0m \u001b[38;5;66;03m#3. call Function : update_inventory(customer_orders, inventory)\u001b[39;00m\n\u001b[1;32m---> 16\u001b[0m update_inventory(get_customer_orders(), initialize_inventory(products))\n\u001b[0;32m 18\u001b[0m \u001b[38;5;66;03m#4. call Function : calculate_order_statistics(customer_orders, products)\u001b[39;00m\n\u001b[0;32m 19\u001b[0m order_statistics \u001b[38;5;241m=\u001b[39m calculate_order_statistics(customer_orders\u001b[38;5;241m=\u001b[39mget_customer_orders(), products\u001b[38;5;241m=\u001b[39mproducts)\n", + "Cell \u001b[1;32mIn[98], line 8\u001b[0m, in \u001b[0;36mget_customer_orders\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m product \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m product \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpurchase\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m----> 8\u001b[0m product \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a product name to order (or type \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPURCHASE\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m to finish): \u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mstrip()\u001b[38;5;241m.\u001b[39mlower()\n\u001b[0;32m 10\u001b[0m \u001b[38;5;66;03m# Empty input\u001b[39;00m\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m product \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n", + "File \u001b[1;32mc:\\Users\\sboub\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1282\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 1280\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 1281\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[1;32m-> 1282\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_input_request(\n\u001b[0;32m 1283\u001b[0m \u001b[38;5;28mstr\u001b[39m(prompt),\n\u001b[0;32m 1284\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_parent_ident[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[0;32m 1285\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_parent(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mshell\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m 1286\u001b[0m password\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 1287\u001b[0m )\n", + "File \u001b[1;32mc:\\Users\\sboub\\anaconda3\\Lib\\site-packages\\ipykernel\\kernelbase.py:1325\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 1322\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m 1323\u001b[0m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[0;32m 1324\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m-> 1325\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1326\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m 1327\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n", + "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" + ] + } + ], "source": [ "#7. call all functions above in the appropriate sequence to execute the program and manage customer orders:\n", "\n", @@ -319,20 +347,20 @@ "\n", "# call Functions order:\n", "#1. call Function : initialize_inventory(products)\n", - "inventory = initialize_inventory(products)\n", + "initialize_inventory(products)\n", "\n", "#2. call Function : get_customer_orders()\n", "get_customer_orders()\n", - "list_customer_orders = get_customer_orders()\n", + "\n", "\n", "#3. call Function : update_inventory(customer_orders, inventory)\n", - "update_inventory(list_customer_orders, inventory)\n", + "update_inventory(get_customer_orders(), initialize_inventory(products))\n", "\n", "#4. call Function : calculate_order_statistics(customer_orders, products)\n", - "order_statistics = calculate_order_statistics(list_customer_orders, products)\n", + "order_statistics = calculate_order_statistics(customer_orders=get_customer_orders(), products=products)\n", "\n", "#5. call Function : print_order_statistics(order_statistics)\n", - "#print_order_statistics(order_statistics)\n", + "print_order_statistics(order_statistics)\n", "\n", "#6. call Function : print_updated_inventory(inventory)\n", "print_updated_inventory(inventory)"