diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..7a9c403 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,262 @@ "\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": 1, + "id": "f4ef1ac0-1b48-46a0-a6f2-d1a6402829fc", + "metadata": {}, + "outputs": [], + "source": [ + "products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e3a34eb1-f139-4b00-aafd-ce720e527f2c", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f828ae3b-594e-4a06-9985-549f16d57577", + "metadata": {}, + "outputs": [], + "source": [ + "#2.\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price for {product}: \"))\n", + " if price >= 0:\n", + " total_price += price\n", + " valid_input = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid price.\")\n", + "\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "362fc934-4d72-4dea-8129-a4cf5920958e", + "metadata": {}, + "outputs": [], + "source": [ + "#3.\n", + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + "\n", + " # Number of orders\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " number_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_orders > 0:\n", + " valid_input = True\n", + " else:\n", + " print(\"Number of orders must be greater than zero.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " # Product names\n", + " for _ in range(number_orders):\n", + " valid_input = False\n", + " while not valid_input:\n", + " product = input(\"Enter the name of a product: \").lower()\n", + "\n", + " if product in inventory and inventory[product] > 0:\n", + " customer_orders.add(product)\n", + " valid_input = True\n", + " elif product not in inventory:\n", + " print(\"Product not found in inventory.\")\n", + " else:\n", + " print(\"Product out of stock.\")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3d8423bf-07a5-4940-8157-f3eb31f6f3ec", + "metadata": {}, + "outputs": [], + "source": [ + "#4." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "098eaae3-227b-4400-9aea-96c97c0ae012", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: j\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 4\n", + "Enter the quantity of mugs available: 0\n", + "Enter the quantity of hats available: 1\n", + "Enter the quantity of books available: r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of books available: 5\n", + "Enter the quantity of keychains available: 6\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "a26736ec-2bee-45f2-a8cd-068d3dc29529", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of a product: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product not found in inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Product out of stock.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: book\n", + "Enter the name of a product: hat\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "a3c6c385-03f5-4cea-9143-74c7012d72df", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price for book: 10\n", + "Enter the price for hat: 5\n" + ] + } + ], + "source": [ + "total_price = calculate_total_price(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "a76e99f3-eafe-44e5-896c-64eec0e4119f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer orders: {'book', 'hat'}\n", + "Total price: 15.0\n" + ] + } + ], + "source": [ + "print(\"Customer orders:\", customer_orders)\n", + "print(\"Total price:\", total_price)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c7da5782-4a97-4e53-8ad8-1ad689de1420", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +346,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,