TheAlgorithms/Python · error · ValueError
Exactly one argument must be 0
Error message
Exactly one argument must be 0
What it means
Thrown by electric_power() in electronics/electric_power.py when the number of zeros among (voltage, current, power) is not exactly 1. The function solves P = V*I for the single unknown, signaled by passing 0 for that argument; zero for none or for two arguments is ambiguous and rejected. Returns a Result(name, value) namedtuple.
Source
Thrown at electronics/electric_power.py:41
>>> electric_power(voltage=2, current=4, power=2)
Traceback (most recent call last):
...
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 exactly one argument as 0 — the quantity to compute — and non-zero values for the other two.
- Remember power is computed as abs(voltage * current); the API does not encode load direction.
- Use keyword arguments so the unknown is obvious at the call site.
Example fix
# before electric_power(voltage=2.2, current=2.2, power=4.84) # all supplied # after electric_power(voltage=2.2, current=2.2, power=0) # -> Result(name='power', value=4.84)
Defensive patterns
Strategy: validation
Validate before calling
args = (voltage, current, power)
if args.count(0) != 1:
raise ValueError("pass exactly one 0 as the unknown")
result = electric_power(*args) # Result(name='power'|'voltage'|'current', value=...) Prevention
- 0 is the 'solve for me' marker, not a physical zero — do not default-fill it.
- Unpack via result.name / result.value (Result namedtuple).
- Power is returned as abs(voltage*current); direction must be tracked elsewhere.
When it happens
Trigger: electric_power(voltage=2.2, current=2.2, power=4.84) (no zero — all supplied) or electric_power(voltage=0, current=0, power=6) (two zeros). Contrast: electric_power(voltage=2.2, current=2.2, power=0) works and returns Result(name='power', value=4.84).
Common situations: New callers assuming a 3-required-argument API; treating 0W as a physical 'no power' query instead of the 'solve for power' marker; config systems defaulting unset fields to 0.
Related errors
- One and only one argument must be 0
- You cannot supply more or less than 2 values
- Capacitor at index {index} has a negative or zero value!
- You cannot supply more or less than 2 values
- Electron concentration cannot be negative in a semiconductor
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/eae16f9f70611435.
Report an issue: GitHub.