diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..03b0db2 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,149 @@ "\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": 1, + "id": "81226195", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] # list of products\n", + "\n", + "\n", + "def initialize_inventory(products): # error handling enhancement\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", + "\n", + "\n", + "def get_customer_orders(): # error handling enhancement\n", + " # set comprehension\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_input = False\n", + " while not valid_input:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " if product in inventory and inventory[product] > 0:\n", + " customer_orders.add(product)\n", + " valid_input = True\n", + " else:\n", + " print(\"Invalid product name or insufficient stock. Please enter a valid product name.\")\n", + " return customer_orders\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " # update quantities with comprehension\n", + " updated_inventory = {\n", + " product: (qty - 1 if product in customer_orders else qty)\n", + " for product, qty in inventory.items()\n", + " }\n", + "\n", + " # remove products with zero quantity (filter with comprehension)\n", + " updated_inventory = {product: qty for product, qty in updated_inventory.items() if qty > 0}\n", + "\n", + " return updated_inventory\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_products = (len(customer_orders) / len(products)) * 100 if products else 0\n", + " return total_products_ordered, percentage_unique_products\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_unique_products = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage_unique_products:.1f}\")\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")\n", + "\n", + "\n", + "def calculate_total_price(customer_orders): #error handling enhancement\n", + " # dict comprehension + sum\n", + " prices = {}\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price >= 0:\n", + " prices[product] = price\n", + " valid_input = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid price.\")\n", + " total_price = sum(prices.values())\n", + " return total_price\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "4e476321", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product name or insufficient stock. Please enter a valid product name.\n", + "Invalid product name or insufficient stock. Please enter a valid product name.\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 2\n", + "book: 1\n", + "keychain: 2\n", + "\n", + "Total Price: 12.0\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"\\nTotal Price: {total_price}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +228,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,