diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..00f474a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,167 @@ "\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": "1436ad04", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Please enter a valid whole number for the count.\n", + "Error: Inventory count cannot be negative. Please try again.\n", + "Error: Inventory count cannot be negative. Please try again.\n", + "\n", + "Initial Inventory: {'t-shirt': 34, 'mug': 22, 'hat': 90, 'book': 33, 'keychain': 44}\n", + "Error: Sorry, 'hafdfd' is not in our product list. Available: t-shirt, mug, hat, book, keychain\n", + "Error: Sorry, 'fefoe' is not in our product list. Available: t-shirt, mug, hat, book, keychain\n", + "\n", + "Order Summary: {'hat', 'book', 'mug', 'keychain'}\n", + "Total Unique Products Ordered: 4\n", + "Percentage of Catalog Ordered: 80.00%\n", + "\n", + "Processing Orders...\n", + "Error: Please enter a valid numeric value for the price.\n", + "\n", + "Final Transaction Total: $144.00\n", + "\n", + "Final Inventory Status:\n", + "- T-shirt: 34\n", + "- Mug: 21\n", + "- Hat: 89\n", + "- Book: 32\n", + "- Keychain: 43\n" + ] + } + ], + "source": [ + "import time\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " count = int(input(f\"Enter the inventory count for {product}: \"))\n", + " if count < 0:\n", + " print(\"Error: Inventory count cannot be negative. Please try again.\")\n", + " continue\n", + " inventory[product] = count\n", + " break \n", + " except ValueError:\n", + " print(\"Error: Please enter a valid whole number for the count.\")\n", + " return inventory\n", + "\n", + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + "\n", + " try:\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"\\nHow many different products would you like to order? \"))\n", + " if num_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative.\")\n", + " continue\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid number for the quantity of orders.\")\n", + "\n", + " for i in range(num_orders):\n", + " while True:\n", + " order = input(f\"Enter the name of product {i+1}: \").strip().lower()\n", + " try:\n", + " if order not in inventory:\n", + " print(f\"Error: Sorry, '{order}' is not in our product list. Available: {', '.join(inventory.keys())}\")\n", + " elif inventory[order] <= 0:\n", + " print(f\"Error: Sorry, '{order}' is currently out of stock.\")\n", + " else:\n", + " customer_orders.add(order)\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid number for the quantity of orders.\")\n", + " \n", + " except Exception as e:\n", + " print(f\"An error occurred while processing orders: {e}\")\n", + " \n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " print(\"\\nProcessing Orders...\")\n", + " for item in customer_orders:\n", + " if inventory.get(item, 0) > 0:\n", + " inventory[item] -= 1\n", + " if inventory[item] == 0:\n", + " print(f\"Alert: {item} is now out of stock!\")\n", + "\n", + "def calculate_order_statistics(customer_orders, total_products_count):\n", + " unique_products_ordered = len(set(customer_orders))\n", + " percentage_ordered = (unique_products_ordered / total_products_count) * 100\n", + " return unique_products_ordered, percentage_ordered\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " unique_orders = set(customer_orders)\n", + " \n", + " prices = {}\n", + " for item in unique_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price for {item}: \"))\n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative.\")\n", + " continue\n", + " prices[item] = price\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid numeric value for the price.\")\n", + " \n", + " for item in customer_orders:\n", + " total_price += prices[item]\n", + " \n", + " return total_price\n", + "\n", + "def main():\n", + " products_list = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + " try:\n", + " inventory = initialize_inventory(products_list)\n", + " print(f\"\\nInitial Inventory: {inventory}\")\n", + "\n", + " customer_orders = get_customer_orders(inventory)\n", + " \n", + " if not customer_orders:\n", + " print(\"No orders were placed !.\")\n", + " return\n", + " total_unique, percentage = calculate_order_statistics(customer_orders, len(products_list))\n", + " print(f\"\\nOrder Summary: {customer_orders}\")\n", + " print(f\"Total Unique Products Ordered: {total_unique}\")\n", + " print(f\"Percentage of Catalog Ordered: {percentage:.2f}%\")\n", + " \n", + " time.sleep(2)\n", + " update_inventory(customer_orders, inventory)\n", + " \n", + " total_price = calculate_total_price(customer_orders)\n", + " print(f\"\\nFinal Transaction Total: ${total_price:.2f}\")\n", + " \n", + " print(\"\\nFinal Inventory Status:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"- {product.capitalize()}: {quantity}\")\n", + "\n", + " except Exception as e:\n", + " print(f\"A critical system error occurred: {e}\")\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +246,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.12" } }, "nbformat": 4,