TheAlgorithms/Python · error · ValueError
All resistance values must be positive
Error message
All resistance values must be positive
What it means
Raised by wheatstone_solver() in electronics/wheatstone_bridge.py when any of resistance_1, resistance_2, resistance_3 is <= 0. A Wheatstone bridge requires three known positive resistances to solve for the fourth (Rx = (R2/R1)*R3); zero or negative values make the ratio undefined or unphysical, hence the single guard covering all three parameters.
Source
Thrown at electronics/wheatstone_bridge.py:33
---------------
Usage examples:
>>> wheatstone_solver(resistance_1=2, resistance_2=4, resistance_3=5)
10.0
>>> wheatstone_solver(resistance_1=356, resistance_2=234, resistance_3=976)
641.5280898876405
>>> wheatstone_solver(resistance_1=2, resistance_2=-1, resistance_3=2)
Traceback (most recent call last):
...
ValueError: All resistance values must be positive
>>> wheatstone_solver(resistance_1=0, resistance_2=0, resistance_3=2)
Traceback (most recent call last):
...
ValueError: All resistance values must be positive
"""
if resistance_1 <= 0 or resistance_2 <= 0 or resistance_3 <= 0:
raise ValueError("All resistance values must be positive")
else:
return float((resistance_2 / resistance_1) * resistance_3)
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Supply all three known arm resistances as positive numbers.
- Check measurement plumbing when a channel reads 0 — that indicates an open input, not a valid resistance.
- Validate all three at once upstream: `if min(r1, r2, r3) <= 0: raise` with a message naming the parameter.
Example fix
# before wheatstone_solver(resistance_1=2, resistance_2=-1, resistance_3=2) # ValueError # after rx = wheatstone_solver(resistance_1=2, resistance_2=1.0, resistance_3=2)
Defensive patterns
Strategy: validation
Validate before calling
if min(resistance_1, resistance_2, resistance_3) <= 0:
raise MeasurementError("All three known bridge arms must be positive") Type guard
def valid_bridge_arms(r1: float, r2: float, r3: float) -> bool:
return min(r1, r2, r3) > 0 Try / catch
try:
rx = wheatstone_solver(resistance_1=r1, resistance_2=r2, resistance_3=r3)
except ValueError as exc:
rx = float("nan")
flag_open_channel(exc) Prevention
- Treat a 0-ohm arm reading as an open/unconnected probe, not data.
- Validate all three arms in one pre-check.
- Log which arm is bad by validating individually first.
When it happens
Trigger: wheatstone_solver(resistance_1=2, resistance_2=-1, resistance_3=2) or any parameter equal to 0; one arm left unset and defaulting to 0 in a form or CLI.
Common situations: Instrumentation setups where one arm's measured value is 0 because the bridge is unbalanced/unconnected; negative values from calibration offsets; forgetting that all three known arms must be supplied.
Related errors
- 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
- Hole concentration cannot be negative in a semiconductor
- Intrinsic concentration cannot be negative in a semiconducto
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/086bdc8028f0232f.
Report an issue: GitHub.