diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb new file mode 100644 index 0000000..8b836b0 --- /dev/null +++ b/lab-python-error-handling.ipynb @@ -0,0 +1,177 @@ +{ + "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": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True: \n", + " try:\n", + " quant = int(input(f\"Enter the quantity of {product}: \"))\n", + " if quant < 0:\n", + " print(\"Quantity cannot be negative.\")\n", + " continue\n", + " inventory[product] = quant\n", + " break \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number for quantity.\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "4c817d09", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c78e3b12", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid number for quantity.\n", + "Quantity cannot be negative.\n", + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "95d12ebe", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " orders = set()\n", + " \n", + " while True:\n", + " try:\n", + " order = input(\"Enter the name of a product to order: \").strip()\n", + " \n", + " if not order or order.isdigit() or (order.startswith('-') and order[1:].isdigit()):\n", + " raise ValueError(\"Product name cannot be empty or a number. Please enter a valid product name.\")\n", + " \n", + " if order not in inventory:\n", + " print(\"The product is not available in the inventory.\")\n", + " continue\n", + " \n", + " orders.add(order)\n", + "\n", + " except ValueError as e:\n", + " print(e)\n", + " \n", + " else:\n", + " more = input(\"Do you want to order another product? (yes/no): \").strip().lower()\n", + " if more != \"yes\":\n", + " break\n", + "\n", + " return orders" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "53587a25", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product name cannot be empty or a number. Please enter a valid product name.\n", + "Product name cannot be empty or a number. Please enter a valid product name.\n", + "Product name cannot be empty or a number. Please enter a valid product name.\n", + "The product is not available in the inventory.\n", + "Customer orders: {'mug'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders(inventory)\n", + "print(\"Customer orders:\", customer_orders)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}