diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..7b3f432 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,324 @@ "\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": 5, + "id": "0b2e919d", + "metadata": {}, + "outputs": [], + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d8aa1252", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_price = False\n", + "\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + "\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + "\n", + " valid_price = True\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " total_price += price\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "79197f4a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: could not convert string to float: 'abc'\n", + "Error: Price cannot be negative.\n", + "Total Price: 20.0\n" + ] + } + ], + "source": [ + "customer_orders = {\"hat\", \"keychain\"}\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print(f\"Total Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "dbbb29c6", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_number = False\n", + "\n", + " while not valid_number:\n", + " try:\n", + " number_of_orders = int(\n", + " input(\"Enter the number of customer orders: \")\n", + " )\n", + "\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + "\n", + " valid_number = True\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = set()\n", + "\n", + " for _ in range(number_of_orders):\n", + " valid_product = False\n", + "\n", + " while not valid_product:\n", + " try:\n", + " product = input(\n", + " \"Enter the name of a product that a customer wants to order: \"\n", + " ).lower()\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\"Product is not available in the inventory.\")\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\"Product is out of stock.\")\n", + "\n", + " if product in customer_orders:\n", + " raise ValueError(\"Product has already been ordered.\")\n", + "\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1e03748e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n", + "Quantity cannot be negative. Please enter a valid quantity.\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9e561b91", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'abc'\n", + "Error: Number of orders cannot be negative.\n", + "Error: Product is not available in the inventory.\n", + "Error: Product has already been ordered.\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "11c3120f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: could not convert string to float: 'abc'\n", + "Error: Price cannot be negative.\n" + ] + } + ], + "source": [ + "total_price = calculate_total_price(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "359d6538", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + "\n", + " inventory = {\n", + " product: quantity - 1 if product in customer_orders else quantity\n", + " for product, quantity in inventory.items()\n", + " }\n", + "\n", + " inventory = {\n", + " product: quantity\n", + " for product, quantity in inventory.items()\n", + " if quantity > 0\n", + " }\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "17ef8cea", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + "\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + "\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6cd80c7a", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of Unique Products Ordered: {order_statistics[1]:.1f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "c108b090", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + "\n", + " inventory_lines = [\n", + " f\"{product}: {quantity}\"\n", + " for product, quantity in inventory.items()\n", + " ]\n", + "\n", + " print(\"\\n\".join(inventory_lines))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "e6c400b1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n", + "Quantity cannot be negative. Please enter a valid quantity.\n", + "Error: invalid literal for int() with base 10: 'abc'\n", + "Error: Number of orders cannot be negative.\n", + "Error: Product is not available in the inventory.\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Unique Products Ordered: 60.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 9\n", + "hat: 9\n", + "book: 9\n", + "keychain: 10\n", + "Error: could not convert string to float: 'abc'\n", + "Error: Price cannot be negative.\n", + "Total Price: 35.4\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print(f\"Total Price: {total_price}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +403,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,