diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..c905bd0 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,139 @@ "\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": "18803f39", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory initialized: {'apple': 3, 'banana': 5, 'cherry': 2}\n", + "Total price of products: $51.00\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Insufficient stock for apple. Only 3 available.\n", + "Error: Insufficient stock for apple. Only 3 available.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n", + "Error: Product not found in inventory. Please enter a valid product.\n" + ] + } + ], + "source": [ + "# Step 1: Define the function for initializing the inventory with error handling\n", + "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", + " inventory[product] = quantity\n", + " valid_quantity = True # Exit the loop once valid input is given\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n", + "\n", + "# Step 2: Modify the calculate_total_price function to include error handling\n", + "def calculate_total_price(products, inventory):\n", + " total_price = 0\n", + " for product in products:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}s: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n", + " total_price += price * inventory.get(product, 0)\n", + " valid_price = True # Exit the loop once valid price is given\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return total_price\n", + "\n", + "# Step 3: Modify the get_customer_orders function to include error handling\n", + "def get_customer_orders(inventory):\n", + " orders = []\n", + " valid_order_count = False\n", + " while not valid_order_count:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of orders: \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative!\")\n", + " valid_order_count = True # Exit the loop once valid order count is entered\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " \n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product = input(\"Enter the product name: \")\n", + " if product not in inventory:\n", + " print(\"Error: Product not found in inventory. Please enter a valid product.\")\n", + " elif inventory[product] <= 0:\n", + " print(f\"Error: {product} is out of stock. Please choose another product.\")\n", + " else:\n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s to order: \"))\n", + " if quantity <= 0:\n", + " raise ValueError(\"Quantity must be a positive integer!\")\n", + " if quantity > inventory[product]:\n", + " raise ValueError(f\"Insufficient stock for {product}. Only {inventory[product]} available.\")\n", + " inventory[product] -= quantity # Reduce stock after successful order\n", + " orders.append((product, quantity))\n", + " valid_quantity = True # Exit the loop once valid quantity is entered\n", + " valid_product = True # Exit the loop once valid product is chosen\n", + " return orders\n", + "\n", + "# Example usage of the functions:\n", + "products = ['apple', 'banana', 'cherry']\n", + "inventory = initialize_inventory(products)\n", + "print(\"Inventory initialized:\", inventory)\n", + "\n", + "# Assuming prices are set for each product\n", + "total_price = calculate_total_price(products, inventory)\n", + "print(f\"Total price of products: ${total_price:.2f}\")\n", + "\n", + "# Get customer orders\n", + "orders = get_customer_orders(inventory)\n", + "print(\"Customer orders:\", orders)\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +218,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.3" } }, "nbformat": 4,