TheAlgorithms/Python · error · ValueError

Inductance must be positive.

Error message

Inductance must be positive.

What it means

Thrown by charging_inductor() when inductance <= 0. Inductance appears in the exponent (-t*R/L); zero or negative L is non-physical for an RL charging model and would divide by zero or invert the curve, so it is rejected (last of three sequential guards).

Solutions

  1. Pass inductance in henries as a positive number (e.g. 0.001 for 1 mH).
  2. Validate unit conversion at your data boundary before calling.
  3. Remember source_voltage and resistance already passed their checks when you see this error.

Example fix

# before
charging_inductor(source_voltage=15, resistance=25, inductance=0, time=5)

# after
charging_inductor(source_voltage=15, resistance=25, inductance=0.02, time=5)
Defensive patterns

Strategy: validation

Validate before calling

if inductance <= 0:
    raise ValueError("inductance must be positive henries")
i = charging_inductor(source_voltage, resistance, inductance, time)

Prevention

When it happens

Trigger: charging_inductor(source_voltage=15, resistance=25, inductance=0, time=5) or any negative inductance.

Common situations: Unit errors (mH/uH/nH vs H) collapsing to 0; forgetting to convert a datasheet value; passing a component count or tolerance percentage instead of henries.

Related errors


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

Appendix: source

Thrown at electronics/charging_inductor.py:88

    ValueError: Source voltage must be positive.

    >>> charging_inductor(source_voltage=10,resistance=0,inductance=20,time=5)
    Traceback (most recent call last):
        ...
    ValueError: Resistance must be positive.

    >>> charging_inductor(source_voltage=15, resistance=25, inductance=0, time=5)
    Traceback (most recent call last):
        ...
    ValueError: Inductance must be positive.
    """

    if source_voltage <= 0:
        raise ValueError("Source voltage must be positive.")
    if resistance <= 0:
        raise ValueError("Resistance must be positive.")
    if inductance <= 0:
        raise ValueError("Inductance must be positive.")
    return round(
        source_voltage / resistance * (1 - exp((-time * resistance) / inductance)), 3
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)