TheAlgorithms/Python · error · ValueError
Resistance cannot be negative
Error message
Resistance cannot be negative
What it means
Thrown by ohms_law() when resistance < 0. Resistance is a magnitude in ohms and cannot be negative, so after the one-zero sentinel check the function rejects negative resistance before computing. Voltage and current are allowed to be negative (direction/sign conventions), resistance is not.
Source
Thrown at electronics/ohms_law.py:28
>>> 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 resistance as a positive value in ohms.
- If resistance came from V/I with a tiny I, clamp or re-measure rather than passing the noisy negative result.
- Use abs() only when the negative sign is known to be spurious.
Example fix
# before
ohms_law(voltage=0, current=1, resistance=-2) # ValueError: Resistance cannot be negative
# after
ohms_law(voltage=0, current=1, resistance=2) # {'voltage': 2.0} Defensive patterns
Strategy: validation
Validate before calling
if resistance < 0:
raise ValueError('resistance must be >= 0') Type guard
def valid_resistance(r: float) -> bool:
return r >= 0 Try / catch
try:
out = ohms_law(v, i, r)
except ValueError as exc:
if 'Resistance cannot be negative' in str(exc):
# noisy V/I measurement produced negative R — re-measure or clamp
...
raise Prevention
- Never pass negative resistance; if computed as V/I, guard tiny denominators.
- Check for stray minus signs in parsed input.
- Only abs() when the sign is provably spurious.
When it happens
Trigger: ohms_law(voltage=0, current=1, resistance=-2); ohms_law(current=0, voltage=-10, resistance=-5) — any call with resistance < 0 and exactly one zero argument.
Common situations: Passing a computed resistance that went negative due to measurement noise or a division by a near-zero current; sign conventions from circuit solvers that use negative resistance for active elements; input typos with a stray minus sign.
Related errors
- One and only one argument must be 0
- 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/d4be23097ea9cf50.
Report an issue: GitHub.