{"record":{"id":"59ed49911094eeed","repo":"TheAlgorithms/Python","slug":"the-height-above-the-ground-cannot-be-negative","errorCode":null,"errorMessage":"The height above the ground cannot be negative","messagePattern":"The height above the ground cannot be negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/potential_energy.py","lineNumber":54,"sourceCode":"    >>> potential_energy(0,5)\n    0.0\n    >>> potential_energy(8,0)\n    0.0\n    >>> potential_energy(10,5)\n    490.3325\n    >>> potential_energy(0,0)\n    0.0\n    >>> potential_energy(2,8)\n    156.9064\n    >>> potential_energy(20,100)\n    19613.3\n    \"\"\"\n    if mass < 0:\n        # handling of negative values of mass\n        raise ValueError(\"The mass of a body cannot be negative\")\n    if height < 0:\n        # handling of negative values of height\n        raise ValueError(\"The height above the ground cannot be negative\")\n    return mass * g * height\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod(name=\"potential_energy\")\n","sourceCodeStart":36,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/potential_energy.py#L36-L62","documentation":"Raised by potential_energy(mass, height) in physics/potential_energy.py when height < 0. Height is measured above the ground reference, so negative heights are rejected; height 0 with any non-negative mass returns 0.0. The mass check runs first, so a negative mass masks a negative height error.","triggerScenarios":"potential_energy(10, -5); height computed as y_object - y_ground with operands swapped, or depth/underground positions passed without rebasing to the chosen reference level.","commonSituations":"World-coordinate systems where ground is not at y=0; choosing a different potential-energy reference level without shifting heights; depth values (below ground, naturally negative) passed as heights.","solutions":["Rebase heights to your reference level: height = y_object - y_reference, ensuring it is >= 0 for the configurations you model","If negative heights are meaningful in your reference frame, shift them: height = y - min_y before calling","Validate height >= 0 where the data enters your program"],"exampleFix":"# before\npotential_energy(10, ground_y - object_y)  # negative when object below ground\n\n# after\npotential_energy(10, max(0.0, object_y - ground_y))  # or rebase reference explicitly","handlingStrategy":"validation","validationCode":"height = object_y - reference_y  # choose the reference explicitly\nif height < 0:\n    raise ValueError(f\"height above reference must be >= 0, got {height}\")\npotential_energy(mass, height)","typeGuard":null,"tryCatchPattern":"try:\n    potential_energy(mass, height)\nexcept ValueError as e:\n    if \"height\" in str(e):\n        height = abs(height)  # or rebase the reference level\n        potential_energy(mass, height)\n    else:\n        raise","preventionTips":["Pick one reference level and always pass height relative to it","Depths below ground are negative — rebase before calling"],"tags":["physics","validation","negative-value","height","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}