diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..77405f5 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,245 @@ "\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": 13, + "id": "79aae7b0-4357-4edb-be61-3c19f302a13f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 1\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 6\n", + "Enter the quantity of books available: 5\n", + "Enter the quantity of keychains available: 10\n", + "Enter the number of customer orders: 2\n", + "Enter the name of a product that a customer wants to order: t-shirt\n", + "Enter the name of a product that a customer wants to order: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory: {'t-shirt': 0, 'mug': 5, 'hat': 6, 'book': 4, 'keychain': 10}\n", + "Customer order: {'t-shirt', 'book'}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {\n", + " product: int(input(f\"Enter the quantity of {product}s available: \"))\n", + " for product in products\n", + "}\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " n = int(input(\"Enter the number of customer orders: \"))\n", + " customer_orders = set()\n", + "\n", + " for _ in range(n):\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").lower()\n", + " customer_orders.add(product)\n", + " inventory[product] -= 1 \n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "customer_order = get_customer_orders(inventory)\n", + "\n", + "print(\"\\nUpdated Inventory:\", inventory)\n", + "print(\"Customer order:\", customer_order)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "40f62d47-392c-4450-897f-29b1754575df", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price. Please enter a valid positive number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price. Please enter a valid positive number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 10\n", + "Enter the price of book: 12\n" + ] + }, + { + "data": { + "text/plain": [ + "22.0" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#2 \n", + "def calculate_total_price(customer_order):\n", + "\n", + " prices = []\n", + "\n", + " for product in customer_order:\n", + " valid = False\n", + " \n", + " while not valid:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " \n", + " if price <= 0:\n", + " raise ValueError(\"Invalid price\")\n", + " \n", + " \n", + " prices.append(price)\n", + " valid = True \n", + " \n", + " except ValueError:\n", + " print(\"Invalid price. Please enter a valid positive number.\")\n", + "\n", + " return sum(prices)\n", + "calculate_total_price(customer_order)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "fea6dd9a-3752-4df9-a7df-e4ee1e00a2eb", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of a product: t-shirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: product out of stock. Choose another product.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: book\n", + "Enter the name of a product: a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: product not found in inventory. Try again.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: product not found in inventory. Try again.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer orders: ['book', 'keychain']\n" + ] + } + ], + "source": [ + "def get_customer_orders(inventory):\n", + " valid = False\n", + " while not valid:\n", + " try:\n", + " n = int(input(\"Enter the number of customer orders: \"))\n", + " if n <= 0:\n", + " print(\"Error: please enter a positive number: \")\n", + " else:\n", + " valid = True\n", + " except ValueError:\n", + " print(\"Error: please enter a valid number of orders: \")\n", + "\n", + " customer_orders = []\n", + "\n", + " for _ in range(n):\n", + " valid = False\n", + " while not valid:\n", + " product = input(\"Enter the name of a product: \").strip().lower()\n", + "\n", + " if product not in inventory: \n", + " print(\"Error: product not found in inventory. Try again.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"Error: product out of stock. Choose another product.\")\n", + " else:\n", + " customer_orders.append(product)\n", + " valid = True\n", + " \n", + " return customer_orders \n", + "\n", + " \n", + "orders = get_customer_orders(inventory)\n", + "print(\"Customer orders:\", orders) \n", + "\n", + " " + ] } ], "metadata": { @@ -90,7 +329,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,