From 1740cbf53617f3368215c07c0d77e2a03f9dfd42 Mon Sep 17 00:00:00 2001 From: bperezlovisolo Date: Thu, 15 Jan 2026 18:11:51 +0100 Subject: [PATCH] lab-python-error-hand --- lab-python-error-handling.ipynb | 181 +++++++++++++++++++++++++++++++- 1 file changed, 179 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..0da651b 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,188 @@ "\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": 108, + "id": "9ce21ae0", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "f1432c59", + "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": 110, + "id": "bc38abad", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "id": "c2a55262", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "id": "30c2176c", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " \n", + " total_price = []\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try: \n", + " product = int((input(\"Enter the price of the product: \")))\n", + " if product >= 0:\n", + " total_price.append(product)\n", + " valid_price = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " product = 0\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + " return sum(total_price)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "id": "21e284c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of products ordered is: 35\n" + ] + } + ], + "source": [ + "total = calculate_total_price(customer_orders)\n", + "print(f\"The total number of products ordered is: {total}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "42d4c06e", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"How many products do you want to order? \"))\n", + " if num_orders > 0:\n", + " break\n", + " else:\n", + " print(\"Order quantity must be greater than zero.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a number.\")\n", + " for i in range(num_orders):\n", + " while True:\n", + " product = input(\"Which product do you want to order? \")\n", + "\n", + " if product not in inventory:\n", + " print(\"That product does not exist. Try again.\")\n", + " elif inventory[product] == 0:\n", + " print(\"Sorry, that product is out of stock.\")\n", + " else:\n", + " customer_orders.add(product)\n", + " break\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "1f13ab37", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "That product does not exist. Try again.\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "c0ec48dc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'hat', 't-shirt', 'keychain', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +267,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,