{"record":{"id":"086bdc8028f0232f","repo":"TheAlgorithms/Python","slug":"all-resistance-values-must-be-positive","errorCode":null,"errorMessage":"All resistance values must be positive","messagePattern":"All resistance values must be positive","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/wheatstone_bridge.py","lineNumber":33,"sourceCode":"    ---------------\n\n    Usage examples:\n    >>> wheatstone_solver(resistance_1=2, resistance_2=4, resistance_3=5)\n    10.0\n    >>> wheatstone_solver(resistance_1=356, resistance_2=234, resistance_3=976)\n    641.5280898876405\n    >>> wheatstone_solver(resistance_1=2, resistance_2=-1, resistance_3=2)\n    Traceback (most recent call last):\n        ...\n    ValueError: All resistance values must be positive\n    >>> wheatstone_solver(resistance_1=0, resistance_2=0, resistance_3=2)\n    Traceback (most recent call last):\n        ...\n    ValueError: All resistance values must be positive\n    \"\"\"\n\n    if resistance_1 <= 0 or resistance_2 <= 0 or resistance_3 <= 0:\n        raise ValueError(\"All resistance values must be positive\")\n    else:\n        return float((resistance_2 / resistance_1) * resistance_3)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":15,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/wheatstone_bridge.py#L15-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nwheatstone_solver(resistance_1=2, resistance_2=-1, resistance_3=2)  # ValueError\n\n# after\nrx = wheatstone_solver(resistance_1=2, resistance_2=1.0, resistance_3=2)","handlingStrategy":"validation","validationCode":"if min(resistance_1, resistance_2, resistance_3) <= 0:\n    raise MeasurementError(\"All three known bridge arms must be positive\")","typeGuard":"def valid_bridge_arms(r1: float, r2: float, r3: float) -> bool:\n    return min(r1, r2, r3) > 0","tryCatchPattern":"try:\n    rx = wheatstone_solver(resistance_1=r1, resistance_2=r2, resistance_3=r3)\nexcept ValueError as exc:\n    rx = float(\"nan\")\n    flag_open_channel(exc)","preventionTips":["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."],"tags":["electronics","wheatstone-bridge","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}