{"record":{"id":"e21cde404dca0b68","repo":"TheAlgorithms/Python","slug":"one-and-only-one-argument-must-be-0-e21cde","errorCode":null,"errorMessage":"One and only one argument must be 0","messagePattern":"One and only one argument must be 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/ohms_law.py","lineNumber":26,"sourceCode":"    and resistance, and then in a Python dict return name/value pair of the zero value.\n\n    >>> 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":8,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/ohms_law.py#L8-L43","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nohms_law(voltage=2, current=2, resistance=5)  # ValueError\n\n# after\nv = ohms_law(voltage=0, current=2, resistance=5)['voltage']  # 10.0","handlingStrategy":"validation","validationCode":"if (voltage, current, resistance).count(0) != 1:\n    raise ValueError('exactly one of V/I/R must be 0 (the unknown)')","typeGuard":"def has_single_unknown(v: float, i: float, r: float) -> bool:\n    return (v, i, r).count(0) == 1","tryCatchPattern":"try:\n    out = ohms_law(v, i, r)\nexcept ValueError as exc:\n    if 'must be 0' in str(exc):\n        # wrong sentinel usage — fix the call, don't retry\n        ...\n    raise","preventionTips":["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."],"tags":["electronics","validation","ohms-law","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}