diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..00c11e6 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,122 @@ "\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": "f9e12830", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " Order Statistics.\n", + "Total Number of Orders: 1\n", + "Perecentage of Unique Products Ordered: 0.2%\n", + "\n", + " Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 1\n", + "hat: 1\n", + "book: 1\n", + "keychain: 1\n", + "\n", + " Total Price: 1.0¢ \n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + "\n", + " inventory = {\n", + " product: get_valied_int(f\"Enter the quantity of {product}s available: \")\n", + " for product in products\n", + " }\n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + " num_orders = get_valied_int(\"Enter the number of customer orders: \")\n", + "\n", + " orders = [\n", + " input(\"Enter the name of the products that the customer want orders: \")\n", + " .strip()\n", + " .lower()\n", + " for _ in range (num_orders)\n", + "\n", + " ]\n", + " return orders\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = {\n", + " product: get_valied_float(f\"Enter the price of {product}: \")\n", + " \n", + " for product in customer_orders\n", + " }\n", + " total = sum(prices.values())\n", + " return total \n", + "\n", + "def update_inventory(inventory, customer_orders):\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " \n", + " new_inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " inventory = new_inventory\n", + " return inventory\n", + "\n", + "def get_valied_int(promt):\n", + " while True:\n", + " try:\n", + " value = int(input(promt))\n", + " if value < 0:\n", + " raise ValueError(\"The value must be a non-negative integer.\")\n", + " return value\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid integer.\")\n", + " \n", + "\n", + "def get_valied_float(promt):\n", + " while True:\n", + " try:\n", + " value = float(input(promt))\n", + " if value < 0:\n", + " raise ValueError(\"The value must be a non-negative integer.\")\n", + " return value\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid integer.\")\n", + " \n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "unique_orders = set(customer_orders)\n", + "total_ordered = len(customer_orders)\n", + "percentage_ordered = (len(unique_orders) / len(products)) * 1\n", + "\n", + "print(f\"\\n Order Statistics.\")\n", + "print(f\"Total Number of Orders: {total_ordered}\")\n", + "print(f\"Perecentage of Unique Products Ordered: {percentage_ordered:.1f}%\")\n", + "\n", + "inventory = update_inventory(inventory, customer_orders)\n", + "\n", + "print(\"\\n Updated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "total_price = calculate_total_price(unique_orders) \n", + "print(f\"\\n Total Price: {total_price}¢ \")\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +201,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,