diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..0f3c477 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,205 @@ "\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": "9ff88855-b8b6-46e7-972a-856507e94e13", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 5\n", + "Enter the quantity of books available: 5\n", + "Enter the quantity of keychains available: 5\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "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\n", + "\n", + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18bccc5d-b48a-4e79-b15d-bc47d14d7474", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid number.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: -1\n" + ] + } + ], + "source": [ + "def get_customer_orders():\n", + " while True:\n", + " try:\n", + " customer_order_no = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " if customer_order_no >= 0: \n", + " list_of_items = [(input(f\"Enter the name of a product that a customer wants to order: \")) for i in range(customer_order_no)]\n", + " return list_of_items\n", + " \n", + " elif customer_order_no < 0:\n", + " pass\n", + " \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + " \n", + " \n", + "\n", + "list_of_items = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7800e76a-f74a-475f-a608-c8d3276d1212", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['mug', 'book']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list_of_items" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "646405ba-6afa-4893-90d0-f040cb494b41", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " while True:\n", + " try:\n", + " customer_order_no = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " if customer_order_no >= 0:\n", + " list_of_items = [\n", + " input(\"Enter the name of a product that a customer wants to order: \")\n", + " for i in range(customer_order_no)\n", + " ]\n", + " return list_of_items\n", + "\n", + " else:\n", + " print(\"Number of orders cannot be negative.\")\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + "list_of_items = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73786c2d-ccc4-409a-ac4d-e0c79bc9b869", + "metadata": {}, + "outputs": [], + "source": [ + "def total_price(list_of_items):\n", + " total = 0\n", + " for item in list_of_items:\n", + " while True:\n", + " try:\n", + " price = int(input(f\"Enter the price for the {item}: \"))\n", + " \n", + " if price >= 0:\n", + " total += price\n", + " break # You need this to move on to the next item\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + " \n", + " print(\"The total price is \", total)\n", + " return total\n", + "\n", + "total_price(list_of_items)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebc64977-09c1-4712-89fd-02ec599890ee", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bb189163-c18a-4fb5-bfeb-14d2aee3dcb3", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +282,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,