{"record":{"id":"21ed2b95b9f02b72","repo":"TheAlgorithms/Python","slug":"power-cannot-be-negative-in-any-electrical-electro","errorCode":null,"errorMessage":"Power cannot be negative in any electrical/electronics system","messagePattern":"Power cannot be negative in any electrical/electronics system","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/electric_power.py","lineNumber":43,"sourceCode":"        ...\n    ValueError: Exactly one argument must be 0\n    >>> electric_power(voltage=0, current=0, power=2)\n    Traceback (most recent call last):\n        ...\n    ValueError: Exactly one argument must be 0\n    >>> electric_power(voltage=0, current=2, power=-4)\n    Traceback (most recent call last):\n        ...\n    ValueError: Power cannot be negative in any electrical/electronics system\n    >>> electric_power(voltage=2.2, current=2.2, power=0)\n    Result(name='power', value=4.84)\n    >>> electric_power(current=0, power=6, voltage=2)\n    Result(name='current', value=3.0)\n    \"\"\"\n    if (voltage, current, power).count(0) != 1:\n        raise ValueError(\"Exactly one argument must be 0\")\n    elif power < 0:\n        raise ValueError(\n            \"Power cannot be negative in any electrical/electronics system\"\n        )\n    elif voltage == 0:\n        return Result(\"voltage\", power / current)\n    elif current == 0:\n        return Result(\"current\", power / voltage)\n    elif power == 0:\n        return Result(\"power\", float(round(abs(voltage * current), 2)))\n    else:\n        raise AssertionError\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":25,"sourceCodeEnd":60,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/electric_power.py#L25-L60","documentation":"Thrown by electric_power() in electronics/electric_power.py when the caller passes a negative value for the `power` argument. The function solves Ohm's power law for whichever of voltage/current/power is 0, and it treats negative power as physically meaningless in this model, so it refuses the input before doing any arithmetic. It is raised only after the 'exactly one argument must be 0' check passes.","triggerScenarios":"Calling electric_power(voltage=2, current=2, power=-4) or any call where the supplied power argument is < 0 while exactly one of the three arguments equals 0 (e.g. electric_power(voltage=0, current=2, power=-8)).","commonSituations":"Passing a signed power reading from a sensor or AC computation directly into the function; confusing apparent/real/reactive power signs; porting code from a library that allowed negative power to represent supplied vs consumed energy.","solutions":["Pass a non-negative value for power (use abs(power) if the sign only encodes direction).","If you actually want power as the output, pass power=0 and non-zero voltage/current: electric_power(voltage=2.2, current=2.2, power=0) returns Result(name='power', value=4.84).","Validate/clamp inputs at the call site before invoking electric_power."],"exampleFix":"# before\nelectric_power(voltage=0, current=2, power=-4)  # ValueError\n\n# after\nres = electric_power(voltage=2, current=2, power=0)  # compute power\n# or, if sign only encodes direction:\nelectric_power(voltage=0, current=2, power=abs(-4))","handlingStrategy":"validation","validationCode":"if power < 0:\n    raise ValueError('power must be >= 0; pass power=0 to compute it')\nif (voltage, current, power).count(0) != 1:\n    raise ValueError('exactly one argument must be 0')","typeGuard":null,"tryCatchPattern":"try:\n    res = electric_power(voltage=v, current=i, power=p)\nexcept ValueError as exc:\n    # covers both negative-power and zero-count messages\n    logger.error('electric_power input rejected: %s', exc)\n    raise","preventionTips":["Treat 0 as 'solve for this quantity', never pass negative power.","Validate power >= 0 at the system boundary before calling.","Wrap solver calls once and map ValueError to your own domain error."],"tags":["electronics","validation","valueerror","power"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}