From f7b539041fa2dbb595946815e20224c4a8545aff Mon Sep 17 00:00:00 2001 From: Ghazal Hassanzadeh Date: Tue, 25 Nov 2025 08:06:26 +0100 Subject: [PATCH] Update lab-python-functions.ipynb --- lab-python-functions.ipynb | 186 ++++++++++++++++++++++++++++++++++++- 1 file changed, 184 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..36ef799 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,193 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "8754fd1c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "list of the products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "#1.\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(f\"list of the products: {products}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "fb436c7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"pleaase enter the quantity of {product}:\"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "inventory = initialize_inventory(products)\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "f58b0d0e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "#2.\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " adding_more = \"yes\" \n", + " \n", + " while adding_more == \"yes\":\n", + " order = input((\"Please enter the product's name that you want to order: \"))\n", + " customer_orders.add(order)\n", + " adding_more = input(\"Add another product? (yes/no): \").lower()\n", + " \n", + " return customer_orders\n", + " \n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "4da679cc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 0, 'hat': 1, 'book': 2, 'keychain': 5}\n" + ] + } + ], + "source": [ + "#3.\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " return inventory\n", + " \n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "7ab4dd73", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 60.0)\n" + ] + } + ], + "source": [ + "#4. \n", + "def calculate_order_statistics(customer_orders, products):\n", + " total = len(customer_orders)\n", + " percentage = (total / len(products)) * 100\n", + " return total, percentage\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "48e2df91", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total unique products ordered: 3\n", + "Percentage of catalog ordered: 60.00%\n" + ] + } + ], + "source": [ + "#5.\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total unique products ordered: {total}\")\n", + " print(f\"Percentage of catalog ordered: {percentage:.2f}%\")\n", + "\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "ae97a88a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 0\n", + "hat: 1\n", + "book: 2\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "#6.\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "print_updated_inventory(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +243,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,