TheAlgorithms/Python · error · ValueError
One and only one argument must be 0
Error message
One and only one argument must be 0
What it means
Thrown by ohms_law() in electronics/ohms_law.py when the count of zero arguments among (voltage, current, resistance) is not exactly one. The function solves V = I*R for whichever quantity is 0 — 0 is the sentinel for the unknown, not a physical value. Note that voltage and current may legitimately be negative (the doctests show {'resistance': -10.0} and {'voltage': -3.0}); only the count of zeros matters here.
Source
Thrown at electronics/ohms_law.py:26
and resistance, and then in a Python dict return name/value pair of the zero value.
>>> ohms_law(voltage=10, resistance=5, current=0)
{'current': 2.0}
>>> ohms_law(voltage=0, current=0, resistance=10)
Traceback (most recent call last):
...
ValueError: One and only one argument must be 0
>>> ohms_law(voltage=0, current=1, resistance=-2)
Traceback (most recent call last):
...
ValueError: Resistance cannot be negative
>>> ohms_law(resistance=0, voltage=-10, current=1)
{'resistance': -10.0}
>>> ohms_law(voltage=0, current=-1.5, resistance=2)
{'voltage': -3.0}
"""
if (voltage, current, resistance).count(0) != 1:
raise ValueError("One and only one argument must be 0")
if resistance < 0:
raise ValueError("Resistance cannot be negative")
if voltage == 0:
return {"voltage": float(current * resistance)}
elif current == 0:
return {"current": voltage / resistance}
elif resistance == 0:
return {"resistance": voltage / current}
else:
raise ValueError("Exactly one argument must be 0")
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Pass exactly one argument as 0: ohms_law(voltage=0, current=2, resistance=5) -> {'voltage': 10.0}.
- Pre-check (voltage, current, resistance).count(0) == 1 for dynamic inputs.
- Skip the call entirely when all three values are already known.
Example fix
# before ohms_law(voltage=2, current=2, resistance=5) # ValueError # after v = ohms_law(voltage=0, current=2, resistance=5)['voltage'] # 10.0
Defensive patterns
Strategy: validation
Validate before calling
if (voltage, current, resistance).count(0) != 1:
raise ValueError('exactly one of V/I/R must be 0 (the unknown)') Type guard
def has_single_unknown(v: float, i: float, r: float) -> bool:
return (v, i, r).count(0) == 1 Try / catch
try:
out = ohms_law(v, i, r)
except ValueError as exc:
if 'must be 0' in str(exc):
# wrong sentinel usage — fix the call, don't retry
...
raise Prevention
- 0 marks the unknown; V and I may be negative, R may not.
- Skip the call when all three are known.
- Wrap dynamic inputs with the count(0)==1 check.
When it happens
Trigger: ohms_law(voltage=2, current=2, resistance=5) (all known); ohms_law(voltage=0, current=0, resistance=5) (two zeros); using None instead of 0 to mark the unknown.
Common situations: Assuming the function validates a fully-specified triple; porting from APIs that used None as a placeholder; batch-processing rows where none or several fields are zero.
Related errors
- Resistance cannot be negative
- Power cannot be negative in any electrical/electronics syste
- One and only one argument must be 0
- One and only one argument must be 0
- Inductance cannot be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/e21cde404dca0b68.
Report an issue: GitHub.