TheAlgorithms/Python · info · ValueError

Exactly one argument must be 0

Error message

Exactly one argument must be 0

What it means

The final else branch of ohms_law() raises 'Exactly one argument must be 0'. It is unreachable: the opening guard `(voltage, current, resistance).count(0) != 1` already raises for every input lacking exactly one zero, so one of voltage==0 / current==0 / resistance==0 always matches afterwards. The branch is defensive residue and its message differs from the primary guard's ('One and only one argument must be 0').

Source

Thrown at electronics/ohms_law.py:36

      ...
    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

  1. Callers need no action — the primary ValueError with the 'One and only one...' message is what fires in practice.
  2. Maintainers: drop the unreachable else or unify the message text with the primary guard.
  3. Coverage tooling: treat as unreachable code, not a test gap.

Example fix

# before
    elif resistance == 0:
        return {"resistance": voltage / current}
    else:
        raise ValueError("Exactly one argument must be 0")

# after
    else:  # resistance == 0, the only remaining case
        return {"resistance": voltage / current}
Defensive patterns

Strategy: validation

Validate before calling

# Guard the reachable primary error instead:
assert (voltage, current, resistance).count(0) == 1

Prevention

When it happens

Trigger: Not reachable via any real inputs; would only execute if the leading count guard were removed or altered.

Common situations: Encountered during source review, refactors, or when aiming for 100% branch coverage; forks that delete the first guard will see the divergent message.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/ee43c00cae6987e1. Report an issue: GitHub.