diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..6a0fe38 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,161 @@ "\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": 4, + "id": "49685fe2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity cannot be negative. Please enter a valid quantity.\n", + "Quantity cannot be negative. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"book\", \"hat\", \"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", + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "895ab404", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 3, 'book': 0, 'hat': 6, 'keychain': 4}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "7a6301ab", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error. Please enter a numeric value.\n", + "Number of product cannot be negative. Please enter the order again.\n", + "Error. papa is not in the inventory\n", + "Sorry book it's out of stock\n" + ] + } + ], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_order = {}\n", + "\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity_orders = int(input(f\"How many products do you want to order: \"))\n", + " if quantity_orders > 0:\n", + " valid_input = True\n", + " else:\n", + " print(\"Number of product cannot be negative. Please enter the order again.\")\n", + " except ValueError: \n", + " print(\"Error. Please enter a numeric value.\")\n", + "\n", + " for o in range(quantity_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " order = input(\"Please enter the name of the product: \")\n", + " stock = inventory[order]\n", + " if stock > 0:\n", + " customer_order[order] = order\n", + " valid_product = True\n", + " else:\n", + " print(f\"Sorry {order} it's out of stock\")\n", + " except KeyError:\n", + " print(f\"Error. {order} is not in the inventory\")\n", + " \n", + " return customer_order\n", + "\n", + "customer_order = get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "5ecc0f4c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Price cannot be a word. Enter a number.\n", + "Price cannot be negative. Enter price again.\n", + "The order's total price is: 20.0\n" + ] + } + ], + "source": [ + "def products_price (customer_order):\n", + " prices = {}\n", + "\n", + " for product in customer_order:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Please introduce the price of {product} in the customer order: \"))\n", + " if price >= 0:\n", + " prices[product] = price\n", + " valid_input = True\n", + "\n", + " else:\n", + " print(\"Price cannot be negative. Enter price again.\")\n", + "\n", + " except ValueError:\n", + " print(\"Price cannot be a word. Enter a number.\")\n", + " \n", + " \n", + " total_price = sum(prices.values())\n", + " print(f\"The order's total price is: {total_price}\")\n", + " \n", + " return total_price \n", + "\n", + "prices = products_price(customer_order) \n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +240,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,