From c22a4fcb96030072896fee2e4e57d979a79b8b05 Mon Sep 17 00:00:00 2001 From: titijoli2-ai Date: Thu, 28 May 2026 17:46:26 +0200 Subject: [PATCH] Add files via upload --- lab-python-error-handling.ipynb | 297 +++++++++++++++++++++++++++++++- 1 file changed, 296 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..c0182e2 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,301 @@ "\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": null, + "id": "2ccde986-c7b9-44b9-b0bd-8a6a6ce00ad3", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d5d8d383-e71c-4840-bb24-1dfdfb32479d", + "metadata": {}, + "outputs": [], + "source": [ + "products =[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "161e02ed-6e0f-4930-99f8-6a3aa16c0481", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "75767945-7bae-4929-973a-58f85a4adbff", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirt available: 34\n", + "Enter the quantity of mug available: 22\n", + "Enter the quantity of hat available: 9\n", + "Enter the quantity of book available: 17\n", + "Enter the quantity of keychain available: 27\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 34, 'mug': 22, 'hat': 9, 'book': 17, 'keychain': 27}\n" + ] + } + ], + "source": [ + " 1.\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} 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", + "inventory = initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "aa82fbf0-4ef2-403d-903a-770064536145", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: mug\n" + ] + }, + { + "data": { + "text/plain": [ + "{'mug'}" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_customer_orders ():\n", + " customer_orders = set ()\n", + " while True:\n", + " product = (input(\"Enter the name of a product to order:\"))\n", + " customer_orders.add(product)\n", + " return customer_orders\n", + " answer = input(\"Do you want to add another product: Yes ou No?\")\n", + " if answer.lower() != \"Yes\":\n", + " break\n", + " print(\"Customer orders are:\", customer_orders) \n", + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8e60d975-a24a-4865-bfef-73a9c5b885bf", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "24b9e4fe-37c6-4b39-adc4-d0ba9b6716f8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug'}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total Price: 10.0\n" + ] + } + ], + "source": [ + "2.\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\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 of {product}: \"))\n", + " if price>= 0:\n", + " total_price += price \n", + " valid_input = True\n", + " else:\n", + " print (\"price cannot be negative\")\n", + " except ValueError :\n", + " print(\"invalid input. please enter a valid price\")\n", + " \n", + " return total_price\n", + "customer_orders = get_customer_orders()\n", + "\n", + "print(customer_orders)\n", + "\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"\\nTotal Price: {total_price}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d054ec7b-fc36-4645-9d49-2c59d0c2ce6d", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "cd3c1014-4139-4beb-b8fd-0d0dafceb1c2", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product to order: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug'}\n" + ] + } + ], + "source": [ + "3.\n", + "\n", + "def get_customers_orders():\n", + " customers_orders = set()\n", + " valid_number = False\n", + " while not valid_numbers:\n", + " try:\n", + " number_orders = int(input(\"enter the number of customer orders\"))\n", + " if number_orders <= 0:\n", + " print(\"Error: number of orders must be positive.\")\n", + " else:\n", + " valid_number = True\n", + " except value_error:\n", + " print(\"Error: please enter a valid number.\")\n", + "\n", + " for _ in range (number_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\"enter product name\")\n", + " if product not in inventory:\n", + " print(\"Error: product does not exist.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"Error: product out of stock.\")\n", + " else:\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " except ValueError:\n", + " print(\"Error: invalid input.\")\n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(customer_orders)\n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "db5d237c-42b6-46cc-9319-644ece66e65b", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d39e49aa-9f45-4827-b97d-d3595129d082", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "636e4e95-09f2-4f25-a127-f9d161bfd0fc", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7dd9bb9a-3d6b-48e0-ac1b-20b83a3c34c6", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e28a0fb3-4c0c-4f49-b892-4e1771b1fcbb", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +385,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.11" } }, "nbformat": 4,