From 76150c7128c8fc1323121ef3613f66633999234a Mon Sep 17 00:00:00 2001 From: Kseniia Iukhlina Date: Fri, 24 Jul 2026 20:01:49 +0200 Subject: [PATCH] The lab 'lab-python-error-handling' is ready to check --- lab-python-error-handling.ipynb | 468 +++++++++++++++++++++++++++++++- 1 file changed, 467 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..4f7fff6 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,472 @@ "\n", "4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n" ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "ab63caa9-fb09-4a70-bd60-75149349e720", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " \"\"\"\n", + " Collect avaliable amount of products in inventory dictionary\n", + "\n", + " Inputs:\n", + " -------\n", + " products: a list with the product names\n", + " inp: input data, as a number of products in stock\n", + " \"\"\"\n", + " inventory ={}\n", + " for product in products:\n", + " input_completed = False\n", + " while not input_completed:\n", + " try:\n", + " inp = int(input(\"Enter the quantity of {}s available: \".format(product)))\n", + " if inp >= 0:\n", + " inventory[product] = inp\n", + " input_completed = True\n", + " else:\n", + " print(\"The entered quantity is negative. Please enter a positive value.\")\n", + " except ValueError:\n", + " print(\"Invalid input type. Please enter a valid quantity.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "0ecdc55d-a37b-4dd2-8759-e9c8b064f81f", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " \"\"\"\n", + " Get the product names and collect them into 'customer_orders' set\n", + "\n", + " Outputs:\n", + " --------\n", + " customer_orders: set with product names\n", + " \"\"\"\n", + " customer_orders = set()\n", + " input_completed_1 = False\n", + " \n", + " while not input_completed_1:\n", + " try:\n", + " while not input_completed_1:\n", + " customer_orders_count = int(input(\"Enter the number of customer orders\").lower().strip())\n", + " if customer_orders_count <= 0:\n", + " print(\"Please, enter the positive number greater than 0\")\n", + " else:\n", + " input_completed_1 = True \n", + " except ValueError:\n", + " print(\"Invalid input type. Please enter a valid quantity.\")\n", + "\n", + " for item in range(1, customer_orders_count + 1):\n", + " input_completed_2 = False\n", + " while not input_completed_2:\n", + " inp = input(\"Enter the name of a product that a customer wants to order\").lower().strip()\n", + " if inp in inventory:\n", + " customer_orders.add(inp)\n", + " input_completed_2 = True\n", + " else:\n", + " print(\"The {} product doesn't exist or out of stock. Plise enter the product name from Inventory dictionary\".format(item))\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "514ea7d3-87e3-43ad-901b-dc1e04a89078", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Update avaliable amount of products in 'inventory' dictionary usin product names from 'customer_orders' set\n", + "\n", + " Inputs:\n", + " -------\n", + " customer_orders: set of product names for order\n", + " inventory: dictionary of product names (keys) and avaliable number of items (values)\n", + " \"\"\"\n", + " for item in customer_orders:\n", + " if item in inventory:\n", + " inventory[item] -= 1\n", + " else:\n", + " print(\"Warning: '{}' not found in inventory\".format(item))\n", + " inventory = {key: count for key, count in inventory.items() if count != 0} \n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "fcd5ca4d-0ff1-4f40-ad6f-b266340a4496", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price_order(customer_orders):\n", + " \"\"\"\n", + " Collects prices for order items\n", + "\n", + " Inputs:\n", + " -------\n", + " customer_orders: set of product names for order\n", + "\n", + " Outputs:\n", + " --------\n", + " list_of_total_prices: list with entered prices\n", + " \"\"\"\n", + " \n", + " total_order_price_list = []\n", + " for item in customer_orders:\n", + " input_completed = False\n", + " while not input_completed:\n", + " try:\n", + " inp = int(input(\"Enter the price of {}\".format(item)))\n", + " if inp >= 0:\n", + " total_order_price_list.append(inp)\n", + " input_completed = True\n", + " else:\n", + " print(\"The entered price is negative. Please enter a positive value.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\") \n", + " total_order_price = sum(total_order_price_list)\n", + " \n", + " return total_order_price" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "d41caecf-5584-49ee-9b66-6c84ff5f02c6", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"\n", + " Calculate order statistics\n", + "\n", + " Inputs:\n", + " -------\n", + " customer_orders: set of product names for order\n", + " products: list of product names \n", + "\n", + " Outputs:\n", + " --------\n", + " order_statistics: tuple of statistical parameters. 'tpo' - Total Products Ordered; 'ppo' - Percentage of Products Ordered, %\n", + " \"\"\"\n", + " tpo = len(customer_orders)\n", + " ppo = int(len(customer_orders)*100/len(products))\n", + " order_statistics = (tpo, ppo)\n", + " return order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1dc8b1b9-946a-4f8b-9be4-353071fff0f2", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " \"\"\"\n", + " Prints order statistics\n", + "\n", + " Inputs:\n", + " -------\n", + " order_statistics: tuple of statistical parameters. 'tpo' - Total Products Ordered; 'ppo' - Percentage of Products Ordered, % \n", + "\n", + " Outputs:\n", + " --------\n", + " order_statistics: the data from tuple of statistical parameters\n", + " \"\"\"\n", + " print(\"Total Products Ordered: {}\".format(order_statistics[0]))\n", + " print(\"Percentage of Products Ordered: {}%\".format(order_statistics[1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "634a8cd8-d193-4819-8503-50daae246a56", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " \"\"\"\n", + " Updates and prints data in 'invenrory' dictionary\n", + "\n", + " Inputs:\n", + " -------\n", + " inventory: dictionary of product names (keys) and avaliable number of items (values) \n", + "\n", + " Outputs:\n", + " --------\n", + " inventory: printed data on 'inventory' dictionary\n", + " \"\"\"\n", + " print(\"Updated Inventory:\")\n", + " \n", + " for p_name, i_count in inventory.items():\n", + " print(f'{p_name}: {i_count}')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "2c2f021a-3ab1-428a-86af-d7b1a159efab", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "00a8c369-9b18-4387-8e8e-894bf0ee3efb", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: we\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input type. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: -234\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The entered quantity is negative. Please enter a positive value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 3\n", + "Enter the quantity of mugs available: 3\n", + "Enter the quantity of hats available: 3\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 3\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "53d89e73-442d-428b-83f9-b52913b5f88c", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders wrsd\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input type. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders -23\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please, enter the positive number greater than 0\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please, enter the positive number greater than 0\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders 2\n", + "Enter the name of a product that a customer wants to order hsdfsdf\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The 1 product doesn't exist or out of stock. Plise enter the product name from Inventory dictionary\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order hat\n", + "Enter the name of a product that a customer wants to order book\n" + ] + }, + { + "data": { + "text/plain": [ + "{'book', 'hat'}" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders = get_customer_orders(inventory)\n", + "customer_orders " + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "19c49a8c-06b4-4fe5-bcbf-61b6d1c041fa", + "metadata": {}, + "outputs": [], + "source": [ + "order_status = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "01d2ed4b-1669-4a16-85a9-9b485a93366a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40%\n" + ] + } + ], + "source": [ + "print_order_statistics(order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "2f138f43-dda6-4770-b271-1ce9a7e02632", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "8b938aa9-6b7d-4c8e-8682-67b6eb830b51", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 3\n", + "mug: 3\n", + "hat: 2\n", + "book: 2\n", + "keychain: 3\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "528309e0-ad1b-425b-ba5b-96b26ba7f14d", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat 200\n", + "Enter the price of book 300\n" + ] + } + ], + "source": [ + "price_list = calculate_total_price_order(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "66e17119-557d-4043-99a6-9b252b139791", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 500\n" + ] + } + ], + "source": [ + "print(\"Total Price: {}\".format(price_list))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "326e3240-5100-45b0-955a-aa8116476d4f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +556,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,