diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..fe93741 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,18 +41,216 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "#Define a list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "#1.Define a function named initialize_inventory that takes products as a parameter, \n", + "# #Implement the code for initializing an inventory dictionary using a loop and user input and \n", + "# apply error handling techniques to each function using try-except blocks.\n", + "\n", + "def initialize_inventory(products):\n", + "\tinventory = {}\n", + "\tfor product in products:\n", + "\t\twhile True:\n", + "\t\t\ttry:\n", + "\t\t\t\tqty = int(input(f\"Enter initial quantity for {product}: \"))\n", + "\t\t\t\tif qty < 0:\n", + "\t\t\t\t\traise ValueError(\"Quantity cannot be negative.\")\n", + "\t\t\t\tinventory[product] = qty\n", + "\t\t\t\tbreak\n", + "\t\t\texcept ValueError as e:\n", + "\t\t\t\tprint(f\"Invalid input: {e}. Please enter a non-negative integer.\")\n", + "\treturn inventory\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e0024e66", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " # Implement the code for prompting the user to enter the products names using a loop\n", + " # apply error handling techniques to each function using try-except blocks.\n", + " orders = []\n", + " print(\"Enter product names one by one. Type 'done' to finish.\")\n", + " while True:\n", + " try:\n", + " product = input(\"Enter product name: \").strip()\n", + " if product.lower() == 'done':\n", + " break\n", + " if product not in products:\n", + " raise ValueError(f\"'{product}' is not a valid product.\")\n", + " orders.append(product)\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " return orders" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "7c6c2a73", + "metadata": {}, + "outputs": [], + "source": [ + "#Define a function named update_inventory that takes customer_orders and inventory as parameters\n", + "# apply error handling techniques to each function using try-except blocks.\n", + "def update_inventory(customer_orders, inventory):\n", + "\tfor product in customer_orders:\n", + "\t\ttry:\n", + "\t\t\tif product not in inventory:\n", + "\t\t\t\traise KeyError(f\"Product '{product}' not found in inventory.\")\n", + "\t\t\tif inventory[product] <= 0:\n", + "\t\t\t\traise ValueError(f\"Product '{product}' is out of stock.\")\n", + "\t\t\tinventory[product] -= 1\n", + "\t\texcept KeyError as ke:\n", + "\t\t\tprint(f\"Error: {ke}\")\n", + "\t\texcept ValueError as ve:\n", + "\t\t\tprint(f\"Error: {ve}\")\n", + "\treturn inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "acd06e6a", + "metadata": {}, + "outputs": [], + "source": [ + "#4. Define a function named calculate_order_statistics that takes customer_orders and products as parameters.\n", + "# apply error handling techniques to each function using try-except blocks.\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\ttry:\n", + "\t\tif not isinstance(customer_orders, list):\n", + "\t\t\traise TypeError(\"customer_orders must be a list.\")\n", + "\t\tif not isinstance(products, list):\n", + "\t\t\traise TypeError(\"products must be a list.\")\n", + "\t\tstats = {}\n", + "\t\tfor product in products:\n", + "\t\t\tstats[product] = customer_orders.count(product)\n", + "\t\treturn stats\n", + "\texcept Exception as e:\n", + "\t\tprint(f\"Error calculating order statistics: {e}\")\n", + "\t\treturn None" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d2376b48", + "metadata": {}, + "outputs": [], + "source": [ + "# Define a function named print_order_statistics that takes customer_orders and products as parameters\n", + "# Implement the code for printing the order statistics\n", + "# Apply error handling techniques to each function using try-except blocks.\n", + "def print_order_statistics(customer_orders, products):\n", + "\ttry:\n", + "\t\tstats = calculate_order_statistics(customer_orders, products)\n", + "\t\tif stats is None:\n", + "\t\t\traise ValueError(\"Could not calculate order statistics.\")\n", + "\t\tprint(\"Order Statistics:\")\n", + "\t\tfor product, count in stats.items():\n", + "\t\t\tprint(f\"{product}: {count}\")\n", + "\texcept Exception as e:\n", + "\t\tprint(f\"Error printing order statistics: {e}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "4403ca7c", + "metadata": {}, + "outputs": [], + "source": [ + "#6. Define a function named print_updated_inventory that takes inventory as a parameter\n", + "def print_updated_inventory(inventory):\n", + " #Implement the code for printing the updated inventory\n", + " # Apply error handling techniques to each function using try-except blocks.\n", + " try:\n", + " if not isinstance(inventory, dict):\n", + " raise TypeError(\"Inventory must be a dictionary.\")\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity} units\")\n", + " except Exception as e:\n", + " print(f\"Error printing updated inventory: {e}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1f0538cb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Welcome to the inventory management system!\n", + "\n", + "\n", + "Inventory initialized.\n", + "\n", + "Enter product names one by one. Type 'done' to finish.\n", + "Error: 'jeans' is not a valid product.\n", + "Error: 'mouse' is not a valid product.\n", + "Order Statistics:\n", + "t-shirt: 0\n", + "mug: 1\n", + "hat: 0\n", + "book: 1\n", + "keychain: 0\n", + "Updated Inventory:\n", + "t-shirt: 4 units\n", + "mug: 2 units\n", + "hat: 5 units\n", + "book: 5 units\n", + "keychain: 5 units\n", + "\n", + "Thank you for using the inventory management system!\n" + ] + } + ], + "source": [ + "#7.Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "# Apply error handling techniques to each function using try-except blocks.\n", + "def main():\n", + " try:\n", + " print(\"\\nWelcome to the inventory management system!\\n\")\n", + " inventory = initialize_inventory(products)\n", + " print(\"\\nInventory initialized.\\n\")\n", + "\n", + " customer_orders = get_customer_orders()\n", + " updated_inventory = update_inventory(customer_orders, inventory)\n", + " \n", + " print_order_statistics(customer_orders, products)\n", + " print_updated_inventory(updated_inventory)\n", + " \n", + " print(\"\\nThank you for using the inventory management system!\")\n", + " except Exception as e:\n", + " print(f\"An unexpected error occurred: {e}\")\n", + "\n", + "if __name__ == \"__main__\":\n", + " try:\n", + " main()\n", + " except Exception as e:\n", + " print(f\"Program terminated due to an error: {e}\")" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -66,7 +264,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,