{"record":{"id":"d4be23097ea9cf50","repo":"TheAlgorithms/Python","slug":"resistance-cannot-be-negative","errorCode":null,"errorMessage":"Resistance cannot be negative","messagePattern":"Resistance cannot be negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/ohms_law.py","lineNumber":28,"sourceCode":"    >>> ohms_law(voltage=10, resistance=5, current=0)\n    {'current': 2.0}\n    >>> ohms_law(voltage=0, current=0, resistance=10)\n    Traceback (most recent call last):\n      ...\n    ValueError: One and only one argument must be 0\n    >>> ohms_law(voltage=0, current=1, resistance=-2)\n    Traceback (most recent call last):\n      ...\n    ValueError: Resistance cannot be negative\n    >>> ohms_law(resistance=0, voltage=-10, current=1)\n    {'resistance': -10.0}\n    >>> ohms_law(voltage=0, current=-1.5, resistance=2)\n    {'voltage': -3.0}\n    \"\"\"\n    if (voltage, current, resistance).count(0) != 1:\n        raise ValueError(\"One and only one argument must be 0\")\n    if resistance < 0:\n        raise ValueError(\"Resistance cannot be negative\")\n    if voltage == 0:\n        return {\"voltage\": float(current * resistance)}\n    elif current == 0:\n        return {\"current\": voltage / resistance}\n    elif resistance == 0:\n        return {\"resistance\": voltage / current}\n    else:\n        raise ValueError(\"Exactly one argument must be 0\")\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":10,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/ohms_law.py#L10-L43","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nohms_law(voltage=0, current=1, resistance=-2)  # ValueError: Resistance cannot be negative\n\n# after\nohms_law(voltage=0, current=1, resistance=2)  # {'voltage': 2.0}","handlingStrategy":"validation","validationCode":"if resistance < 0:\n    raise ValueError('resistance must be >= 0')","typeGuard":"def valid_resistance(r: float) -> bool:\n    return r >= 0","tryCatchPattern":"try:\n    out = ohms_law(v, i, r)\nexcept ValueError as exc:\n    if 'Resistance cannot be negative' in str(exc):\n        # noisy V/I measurement produced negative R — re-measure or clamp\n        ...\n    raise","preventionTips":["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."],"tags":["electronics","validation","ohms-law","resistance","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}