From 9b0d92d7e7cf77858afbac886d48289e9b62ce16 Mon Sep 17 00:00:00 2001 From: Pariya Date: Wed, 16 Sep 2026 20:09:40 +0300 Subject: [PATCH] Complete customer orders lab using functions --- lab-python-functions.ipynb | 197 ++++++++++++++++++++++++++++++++++++- 1 file changed, 195 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..67dc48e 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,204 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "9837aada", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "4dfc0b0a", + "metadata": {}, + "outputs": [], + "source": [ + "#initialize_inventory()\n", + "\n", + "def initialize_inventory(products):\n", + " inventory ={}\n", + " for product in products:\n", + " inventory[product]=int(input(f\"Enter quantity for {product}:\"))\n", + "\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "078c57d3", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "605706f2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 9, 'hat': 5, 'book': 8, 'keychain': 3}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "1e068bfd", + "metadata": {}, + "outputs": [], + "source": [ + "#get_customer_orders\n", + "def get_customer_orders():\n", + " customer_orders= set()\n", + " answer =\"yes\"\n", + " while answer.lower() == \"yes\":\n", + " order=input(\"please enter the product name:\")\n", + " customer_orders.add(order)\n", + " answer=input(\"do you want to add another product?(yes/no):\").lower()\n", + " return customer_orders\n", + "\n", + " \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "4d7bc9a5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'mug'}\n" + ] + } + ], + "source": [ + "order =get_customer_orders()\n", + "print(order)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "a9b39507", + "metadata": {}, + "outputs": [], + "source": [ + "#update_inventory\n", + "def update_inventory(customer_orders,inventory):\n", + " for product in customer_orders:\n", + " inventory[product] = inventory[product] - 1\n", + " return inventory\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "c875c9b4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 8, 'hat': 5, 'book': 7, 'keychain': 3}\n" + ] + } + ], + "source": [ + "updated_inventory = update_inventory(order, inventory)\n", + "print(updated_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "cda360ea", + "metadata": {}, + "outputs": [], + "source": [ + "#calculate_order_statistics\n", + "def calculate_order_statistics(customer_orders,products):\n", + " total_orders = len(customer_orders)\n", + " total_available = len(products)\n", + " percentage = (total_orders/total_available)*100\n", + " return total_orders, percentage\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "636faf0c", + "metadata": {}, + "outputs": [], + "source": [ + "#print_order_statistics\n", + "def print_order_statistics(order_statistics):\n", + " total_orders, percentage = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_orders}\")\n", + " print(f\"Percentage of Products Ordered: {percentage}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f7f6e9d", + "metadata": {}, + "outputs": [], + "source": [ + "#print_updated_inventory\n", + "def print_updated_inventory(inventory):\n", + " print(\"updated_inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8bc4990a", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +254,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,