diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..64a1716 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,157 @@ "\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": "623e27d2", + "metadata": {}, + "outputs": [], + "source": [ + "try:\n", + " products_input = input(\"Enter list of products separated by space\")\n", + " products = products_input.split()\n", + "except:\n", + " raise TypeError(\"Invalid Input\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "46404730", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products: list):\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 quantity in number.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "fe29cb6d", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders: set):\n", + " order_price = []\n", + " for order in customer_orders:\n", + " valid_ = False\n", + " while not valid_:\n", + " try:\n", + " unit_price= int(input(f\"Enter the price of {order}:\"))\n", + " if unit_price > 0:\n", + " order_price.append(unit_price)\n", + " valid_ = True \n", + " else:\n", + " print(\"Unit price cannot be negative or zero. Please enter a valid unit price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter unit price in number.\")\n", + " return sum(order_price)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "20f16e51", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory: dict):\n", + " '''Enter Customers Order'''\n", + " valid_num = False\n", + " customer_orders = []\n", + " while not valid_num:\n", + " try:\n", + " cust_orders_num = int(input(f\"Enter the number of customer orders: \"))\n", + " if cust_orders_num > 0:\n", + " for i in range(0,cust_orders_num): \n", + " valid_prod = False\n", + " while not valid_prod:\n", + " try:\n", + " order_input = input(f\"Enter the name of a product that a customer wants to order:\")\n", + " for key,val in inventory.items():\n", + " if order_input == key and val > 0:\n", + " customer_orders.append(order_input)\n", + " valid_prod = True\n", + " except:\n", + " print(\"Your ordered product is NOT available\") \n", + " \n", + " valid_num = True \n", + " else:\n", + " print(\"Number of orders cannot be negative or zero. Please enter a valid unit price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter unit price in number.\")\n", + " \n", + " return set(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "f90694e7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter quantity in number.\n" + ] + } + ], + "source": [ + "begin_inventory = initialize_inventory(products=products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c52ac4e4", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = get_customer_orders(inventory=begin_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "819d9160", + "metadata": {}, + "outputs": [], + "source": [ + "total_order_price = calculate_total_price(customer_orders=customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bc6ea322", + "metadata": {}, + "outputs": [], + "source": [ + "help(ValueError)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +236,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,