diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..04da34c 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,197 @@ "\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": 6, + "id": "3d67fb50", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "acd370be", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "ae8dafc4", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " print(f\"Select a price for : {product}\")\n", + " \n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}:\"))\n", + "\n", + " if price < 0:\n", + " print(\"Error: price have to be possitive\")\n", + "\n", + " else: \n", + " total_price += price\n", + " valid_price = True\n", + " except ValueError:\n", + " print(\"Error: Enter a valid number please\") \n", + " return total_price \n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "c2a30f0b", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_number = False\n", + " while not valid_number:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of orders: \"))\n", + " if num_orders < 0:\n", + " print(\"Error: Number of order have to be possitive.\")\n", + " else:\n", + " valid_number = True\n", + " except ValueError:\n", + " print(\"Error: Write Enter a valid number please\")\n", + "\n", + " customer_orders = []\n", + "\n", + " for order in range(num_orders):\n", + "\n", + " valid_product = False\n", + "\n", + " while not valid_product:\n", + " \n", + " product_name = input(\"Please enter a product name\").strip().lower()\n", + "\n", + " if product_name in inventory:\n", + "\n", + " if inventory[product_name] > 0:\n", + " customer_orders.append(product_name)\n", + " inventory[product_name] -=1 \n", + " print(f\"{product_name} add to your order\")\n", + " \n", + " valid_product = True\n", + " else:\n", + " print(f\"Error:{product_name} is out of stock\")\n", + " else:\n", + " print(f\"Error: {product_name} is not in our shop\")\n", + "\n", + " return customer_orders\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "a9bfdedb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: ''\n", + "Stock {'t-shirt': 1, 'mug': 1, 'hat': 1, 'book': 1, 'keychain': 1}\n" + ] + } + ], + "source": [ + "my_inventory = initialize_inventory(products)\n", + "print(f\"Stock {my_inventory}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e2db7824", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: dog is not in our shop\n", + "Error:hat is out of stock\n", + "mug add to your order\n", + "book add to your order\n", + "Error:mug is out of stock\n", + "Error:book is out of stock\n", + "keychain add to your order\n", + "Order['mug', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "\n", + "my_orders = get_customer_orders(my_inventory)\n", + "print(f\"Order{my_orders}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dbb49dc8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Select a price for : hat\n", + "Error: Enter a valid number please\n", + "Error: Enter a valid number please\n", + "Total: 21.0\n" + ] + } + ], + "source": [ + "\n", + "total = calculate_total_price(my_orders)\n", + "print(f\"Total: {total}\") " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5e37585d", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +276,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,