diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb new file mode 100644 index 0000000..9c43179 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,664 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Error Handling" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: Practice how to identify, handle and recover from potential errors in Python code using try-except blocks." + ] + }, + { + "cell_type": "markdown", + "id": "e253e768-aed8-4791-a800-87add1204afa", + "metadata": {}, + "source": [ + "## Challenge \n", + "\n", + "Paste here your lab *functions* solutions. Apply error handling techniques to each function using try-except blocks. " + ] + }, + { + "cell_type": "markdown", + "id": "9180ff86-c3fe-4152-a609-081a287fa1af", + "metadata": {}, + "source": [ + "The try-except block in Python is designed to handle exceptions and provide a fallback mechanism when code encounters errors. By enclosing the code that could potentially throw errors in a try block, followed by specific or general exception handling in the except block, we can gracefully recover from errors and continue program execution.\n", + "\n", + "However, there may be cases where an input may not produce an immediate error, but still needs to be addressed. In such situations, it can be useful to explicitly raise an error using the \"raise\" keyword, either to draw attention to the issue or handle it elsewhere in the program.\n", + "\n", + "Modify the code to handle possible errors in Python, it is recommended to use `try-except-else-finally` blocks, incorporate the `raise` keyword where necessary, and print meaningful error messages to alert users of any issues that may occur during program execution.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for t-shirt.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for mug.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for hat.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of books available: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for book.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of keychains available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for keychain.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking number of orders.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: t-shirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: keychhain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product does not exist in inventory.\n", + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n", + "Finished calculating order statistics.\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 5\n", + "Percentage of Unique Products Ordered: 100.0\n", + "Finished printing order statistics.\n", + "Inventory updated successfully.\n", + "Finished inventory update process.\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 3\n", + "hat: 1\n", + "book: 2\n", + "keychain: 1\n", + "Finished printing inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for mug.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for hat.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for book.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for t-shirt.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for keychain.\n", + "Finished calculating total price.\n", + "\n", + "Total Price: 53.0\n", + "\n", + "Program finished.\n" + ] + } + ], + "source": [ + "# Lab | Error Handling\n", + "\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "# 1. Initialize inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(\n", + " input(f\"Enter the quantity of {product}s available: \")\n", + " )\n", + "\n", + " if quantity < 0:\n", + " raise ValueError(\n", + " \"Quantity cannot be negative.\"\n", + " )\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " else:\n", + " inventory[product] = quantity\n", + " break\n", + "\n", + " finally:\n", + " print(f\"Finished checking quantity for {product}.\")\n", + "\n", + " return inventory\n", + "\n", + "\n", + "# 2. Get customer orders\n", + "def get_customer_orders(inventory):\n", + "\n", + " while True:\n", + " try:\n", + " number_of_orders = int(\n", + " input(\"Enter the number of customer orders: \")\n", + " )\n", + "\n", + " if number_of_orders < 0:\n", + " raise ValueError(\n", + " \"Number of orders cannot be negative.\"\n", + " )\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " else:\n", + " break\n", + "\n", + " finally:\n", + " print(\"Finished checking number of orders.\")\n", + "\n", + " customer_orders = set()\n", + "\n", + " for _ in range(number_of_orders):\n", + "\n", + " while True:\n", + " try:\n", + " product = input(\n", + " \"Enter the name of a product: \"\n", + " ).lower()\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\n", + " \"Product does not exist in inventory.\"\n", + " )\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\n", + " \"Product is out of stock.\"\n", + " )\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " else:\n", + " customer_orders.add(product)\n", + " break\n", + "\n", + " finally:\n", + " print(\"Finished checking product.\")\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "# 3. Update inventory\n", + "def update_inventory(customer_orders, inventory):\n", + "\n", + " try:\n", + " updated_inventory = {\n", + " product: quantity - 1\n", + " if product in customer_orders\n", + " else quantity\n", + " for product, quantity in inventory.items()\n", + " }\n", + "\n", + " updated_inventory = {\n", + " product: quantity\n", + " for product, quantity in updated_inventory.items()\n", + " if quantity > 0\n", + " }\n", + "\n", + " except Exception as error:\n", + " print(\"Error while updating inventory:\", error)\n", + " return inventory\n", + "\n", + " else:\n", + " print(\"Inventory updated successfully.\")\n", + " return updated_inventory\n", + "\n", + " finally:\n", + " print(\"Finished inventory update process.\")\n", + "\n", + "\n", + "# 4. Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " try:\n", + " if len(products) == 0:\n", + " raise ValueError(\n", + " \"Product list cannot be empty.\"\n", + " )\n", + "\n", + " total_products_ordered = len(customer_orders)\n", + "\n", + " percentage_unique_products = (\n", + " total_products_ordered / len(products)\n", + " ) * 100\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + " return 0, 0\n", + "\n", + " except ZeroDivisionError:\n", + " print(\"Error: Cannot divide by zero.\")\n", + " return 0, 0\n", + "\n", + " else:\n", + " return (\n", + " total_products_ordered,\n", + " percentage_unique_products\n", + " )\n", + "\n", + " finally:\n", + " print(\"Finished calculating order statistics.\")\n", + "\n", + "\n", + "# 5. Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + "\n", + " try:\n", + " total_products_ordered = order_statistics[0]\n", + " percentage_unique_products = order_statistics[1]\n", + "\n", + " print(\"\\nOrder Statistics:\")\n", + " print(\n", + " \"Total Products Ordered:\",\n", + " total_products_ordered\n", + " )\n", + "\n", + " print(\n", + " \"Percentage of Unique Products Ordered:\",\n", + " percentage_unique_products\n", + " )\n", + "\n", + " except IndexError:\n", + " print(\n", + " \"Error: Order statistics are incomplete.\"\n", + " )\n", + "\n", + " except TypeError:\n", + " print(\n", + " \"Error: Invalid order statistics format.\"\n", + " )\n", + "\n", + " finally:\n", + " print(\"Finished printing order statistics.\")\n", + "\n", + "\n", + "# 6. Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + "\n", + " try:\n", + " print(\"\\nUpdated Inventory:\")\n", + "\n", + " if not inventory:\n", + " raise ValueError(\n", + " \"Inventory is empty.\"\n", + " )\n", + "\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " except AttributeError:\n", + " print(\n", + " \"Error: Inventory must be a dictionary.\"\n", + " )\n", + "\n", + " finally:\n", + " print(\"Finished printing inventory.\")\n", + "\n", + "\n", + "# 7. Calculate total price\n", + "def calculate_total_price(customer_orders):\n", + "\n", + " prices = {}\n", + "\n", + " for product in customer_orders:\n", + "\n", + " while True:\n", + " try:\n", + " price = float(\n", + " input(\n", + " f\"Enter the price of {product}: \"\n", + " )\n", + " )\n", + "\n", + " if price < 0:\n", + " raise ValueError(\n", + " \"Price cannot be negative.\"\n", + " )\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " else:\n", + " prices[product] = price\n", + " break\n", + "\n", + " finally:\n", + " print(\n", + " f\"Finished checking price for {product}.\"\n", + " )\n", + "\n", + " try:\n", + " total_price = sum(\n", + " price\n", + " for price in prices.values()\n", + " )\n", + "\n", + " except TypeError as error:\n", + " print(\n", + " \"Error calculating total price:\",\n", + " error\n", + " )\n", + " return 0\n", + "\n", + " else:\n", + " return total_price\n", + "\n", + " finally:\n", + " print(\"Finished calculating total price.\")\n", + "\n", + "\n", + "# MAIN PROGRAM\n", + "\n", + "try:\n", + "\n", + " inventory = initialize_inventory(products)\n", + "\n", + " customer_orders = get_customer_orders(\n", + " inventory\n", + " )\n", + "\n", + " order_statistics = calculate_order_statistics(\n", + " customer_orders,\n", + " products\n", + " )\n", + "\n", + " print_order_statistics(\n", + " order_statistics\n", + " )\n", + "\n", + " inventory = update_inventory(\n", + " customer_orders,\n", + " inventory\n", + " )\n", + "\n", + " print_updated_inventory(\n", + " inventory\n", + " )\n", + "\n", + " total_price = calculate_total_price(\n", + " customer_orders\n", + " )\n", + "\n", + "except Exception as error:\n", + "\n", + " print(\n", + " \"Unexpected program error:\",\n", + " error\n", + " )\n", + "\n", + "else:\n", + "\n", + " print(\n", + " \"\\nTotal Price:\",\n", + " total_price\n", + " )\n", + "\n", + "finally:\n", + "\n", + " print(\n", + " \"\\nProgram finished.\"\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8796abc1-a0b6-4be8-99e4-5e85ed1293c1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eebfbc51-75ec-427f-b1bf-e533041c46f7", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..9c43179 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,13 +41,603 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for t-shirt.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for mug.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of hats available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for hat.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of books available: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for book.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of keychains available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking quantity for keychain.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking number of orders.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: t-shirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: keychhain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product does not exist in inventory.\n", + "Finished checking product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking product.\n", + "Finished calculating order statistics.\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 5\n", + "Percentage of Unique Products Ordered: 100.0\n", + "Finished printing order statistics.\n", + "Inventory updated successfully.\n", + "Finished inventory update process.\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 3\n", + "hat: 1\n", + "book: 2\n", + "keychain: 1\n", + "Finished printing inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for mug.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: 8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for hat.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for book.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for t-shirt.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Finished checking price for keychain.\n", + "Finished calculating total price.\n", + "\n", + "Total Price: 53.0\n", + "\n", + "Program finished.\n" + ] + } + ], "source": [ - "# your code goes here" + "# Lab | Error Handling\n", + "\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "# 1. Initialize inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(\n", + " input(f\"Enter the quantity of {product}s available: \")\n", + " )\n", + "\n", + " if quantity < 0:\n", + " raise ValueError(\n", + " \"Quantity cannot be negative.\"\n", + " )\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " else:\n", + " inventory[product] = quantity\n", + " break\n", + "\n", + " finally:\n", + " print(f\"Finished checking quantity for {product}.\")\n", + "\n", + " return inventory\n", + "\n", + "\n", + "# 2. Get customer orders\n", + "def get_customer_orders(inventory):\n", + "\n", + " while True:\n", + " try:\n", + " number_of_orders = int(\n", + " input(\"Enter the number of customer orders: \")\n", + " )\n", + "\n", + " if number_of_orders < 0:\n", + " raise ValueError(\n", + " \"Number of orders cannot be negative.\"\n", + " )\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " else:\n", + " break\n", + "\n", + " finally:\n", + " print(\"Finished checking number of orders.\")\n", + "\n", + " customer_orders = set()\n", + "\n", + " for _ in range(number_of_orders):\n", + "\n", + " while True:\n", + " try:\n", + " product = input(\n", + " \"Enter the name of a product: \"\n", + " ).lower()\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\n", + " \"Product does not exist in inventory.\"\n", + " )\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\n", + " \"Product is out of stock.\"\n", + " )\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " else:\n", + " customer_orders.add(product)\n", + " break\n", + "\n", + " finally:\n", + " print(\"Finished checking product.\")\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "# 3. Update inventory\n", + "def update_inventory(customer_orders, inventory):\n", + "\n", + " try:\n", + " updated_inventory = {\n", + " product: quantity - 1\n", + " if product in customer_orders\n", + " else quantity\n", + " for product, quantity in inventory.items()\n", + " }\n", + "\n", + " updated_inventory = {\n", + " product: quantity\n", + " for product, quantity in updated_inventory.items()\n", + " if quantity > 0\n", + " }\n", + "\n", + " except Exception as error:\n", + " print(\"Error while updating inventory:\", error)\n", + " return inventory\n", + "\n", + " else:\n", + " print(\"Inventory updated successfully.\")\n", + " return updated_inventory\n", + "\n", + " finally:\n", + " print(\"Finished inventory update process.\")\n", + "\n", + "\n", + "# 4. Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " try:\n", + " if len(products) == 0:\n", + " raise ValueError(\n", + " \"Product list cannot be empty.\"\n", + " )\n", + "\n", + " total_products_ordered = len(customer_orders)\n", + "\n", + " percentage_unique_products = (\n", + " total_products_ordered / len(products)\n", + " ) * 100\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + " return 0, 0\n", + "\n", + " except ZeroDivisionError:\n", + " print(\"Error: Cannot divide by zero.\")\n", + " return 0, 0\n", + "\n", + " else:\n", + " return (\n", + " total_products_ordered,\n", + " percentage_unique_products\n", + " )\n", + "\n", + " finally:\n", + " print(\"Finished calculating order statistics.\")\n", + "\n", + "\n", + "# 5. Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + "\n", + " try:\n", + " total_products_ordered = order_statistics[0]\n", + " percentage_unique_products = order_statistics[1]\n", + "\n", + " print(\"\\nOrder Statistics:\")\n", + " print(\n", + " \"Total Products Ordered:\",\n", + " total_products_ordered\n", + " )\n", + "\n", + " print(\n", + " \"Percentage of Unique Products Ordered:\",\n", + " percentage_unique_products\n", + " )\n", + "\n", + " except IndexError:\n", + " print(\n", + " \"Error: Order statistics are incomplete.\"\n", + " )\n", + "\n", + " except TypeError:\n", + " print(\n", + " \"Error: Invalid order statistics format.\"\n", + " )\n", + "\n", + " finally:\n", + " print(\"Finished printing order statistics.\")\n", + "\n", + "\n", + "# 6. Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + "\n", + " try:\n", + " print(\"\\nUpdated Inventory:\")\n", + "\n", + " if not inventory:\n", + " raise ValueError(\n", + " \"Inventory is empty.\"\n", + " )\n", + "\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " except AttributeError:\n", + " print(\n", + " \"Error: Inventory must be a dictionary.\"\n", + " )\n", + "\n", + " finally:\n", + " print(\"Finished printing inventory.\")\n", + "\n", + "\n", + "# 7. Calculate total price\n", + "def calculate_total_price(customer_orders):\n", + "\n", + " prices = {}\n", + "\n", + " for product in customer_orders:\n", + "\n", + " while True:\n", + " try:\n", + " price = float(\n", + " input(\n", + " f\"Enter the price of {product}: \"\n", + " )\n", + " )\n", + "\n", + " if price < 0:\n", + " raise ValueError(\n", + " \"Price cannot be negative.\"\n", + " )\n", + "\n", + " except ValueError as error:\n", + " print(\"Error:\", error)\n", + "\n", + " else:\n", + " prices[product] = price\n", + " break\n", + "\n", + " finally:\n", + " print(\n", + " f\"Finished checking price for {product}.\"\n", + " )\n", + "\n", + " try:\n", + " total_price = sum(\n", + " price\n", + " for price in prices.values()\n", + " )\n", + "\n", + " except TypeError as error:\n", + " print(\n", + " \"Error calculating total price:\",\n", + " error\n", + " )\n", + " return 0\n", + "\n", + " else:\n", + " return total_price\n", + "\n", + " finally:\n", + " print(\"Finished calculating total price.\")\n", + "\n", + "\n", + "# MAIN PROGRAM\n", + "\n", + "try:\n", + "\n", + " inventory = initialize_inventory(products)\n", + "\n", + " customer_orders = get_customer_orders(\n", + " inventory\n", + " )\n", + "\n", + " order_statistics = calculate_order_statistics(\n", + " customer_orders,\n", + " products\n", + " )\n", + "\n", + " print_order_statistics(\n", + " order_statistics\n", + " )\n", + "\n", + " inventory = update_inventory(\n", + " customer_orders,\n", + " inventory\n", + " )\n", + "\n", + " print_updated_inventory(\n", + " inventory\n", + " )\n", + "\n", + " total_price = calculate_total_price(\n", + " customer_orders\n", + " )\n", + "\n", + "except Exception as error:\n", + "\n", + " print(\n", + " \"Unexpected program error:\",\n", + " error\n", + " )\n", + "\n", + "else:\n", + "\n", + " print(\n", + " \"\\nTotal Price:\",\n", + " total_price\n", + " )\n", + "\n", + "finally:\n", + "\n", + " print(\n", + " \"\\nProgram finished.\"\n", + " )" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8796abc1-a0b6-4be8-99e4-5e85ed1293c1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eebfbc51-75ec-427f-b1bf-e533041c46f7", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -66,7 +656,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.14" } }, "nbformat": 4,