TheAlgorithms/Python · error · ValueError
Power cannot be negative in any electrical/electronics syste
Error message
Power cannot be negative in any electrical/electronics system
What it means
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.
Source
Thrown at electronics/electric_power.py:43
...
ValueError: Exactly one argument must be 0
>>> electric_power(voltage=0, current=0, power=2)
Traceback (most recent call last):
...
ValueError: Exactly one argument must be 0
>>> electric_power(voltage=0, current=2, power=-4)
Traceback (most recent call last):
...
ValueError: Power cannot be negative in any electrical/electronics system
>>> electric_power(voltage=2.2, current=2.2, power=0)
Result(name='power', value=4.84)
>>> electric_power(current=0, power=6, voltage=2)
Result(name='current', value=3.0)
"""
if (voltage, current, power).count(0) != 1:
raise ValueError("Exactly one argument must be 0")
elif power < 0:
raise ValueError(
"Power cannot be negative in any electrical/electronics system"
)
elif voltage == 0:
return Result("voltage", power / current)
elif current == 0:
return Result("current", power / voltage)
elif power == 0:
return Result("power", float(round(abs(voltage * current), 2)))
else:
raise AssertionError
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
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.
Example fix
# before electric_power(voltage=0, current=2, power=-4) # ValueError # after res = electric_power(voltage=2, current=2, power=0) # compute power # or, if sign only encodes direction: electric_power(voltage=0, current=2, power=abs(-4))
Defensive patterns
Strategy: validation
Validate before calling
if power < 0:
raise ValueError('power must be >= 0; pass power=0 to compute it')
if (voltage, current, power).count(0) != 1:
raise ValueError('exactly one argument must be 0') Try / catch
try:
res = electric_power(voltage=v, current=i, power=p)
except ValueError as exc:
# covers both negative-power and zero-count messages
logger.error('electric_power input rejected: %s', exc)
raise Prevention
- 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.
When it happens
Trigger: 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)).
Common situations: 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.
Related errors
- Exactly one argument must be 0
- One and only one argument must be 0
- One and only one argument must be 0
- Inductance cannot be negative
- Frequency cannot be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/21ed2b95b9f02b72.
Report an issue: GitHub.