TheAlgorithms/Python · error · ValueError
Source voltage must be positive.
Error message
Source voltage must be positive.
What it means
Thrown by charging_inductor() in electronics/charging_inductor.py when source_voltage <= 0. The function computes the RL step response I(t) = (Vs/R)*(1 - e^(-Rt/L)) and requires a strictly positive source voltage; non-positive values are rejected as invalid for a charging model.
Source
Thrown at electronics/charging_inductor.py:84
>>> charging_inductor(source_voltage=0,resistance=200,inductance=20,time=5)
Traceback (most recent call last):
...
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)
Solutions
- Pass a strictly positive DC source voltage.
- If modeling magnitude of a bipolar source, apply abs() deliberately at the call site.
- Use keyword arguments (source_voltage=, resistance=, inductance=, time=) to avoid positional mix-ups.
Example fix
# before charging_inductor(source_voltage=0, resistance=10, inductance=20, time=5) # after charging_inductor(source_voltage=12, resistance=10, inductance=20, time=5)
Defensive patterns
Strategy: validation
Validate before calling
if source_voltage <= 0:
raise ValueError("source_voltage must be positive")
i = charging_inductor(source_voltage, resistance, inductance, time) Prevention
- Note the parameter is named time, unlike charging_capacitor's time_sec.
- This models a DC step, not AC — do not feed signed instantaneous samples.
When it happens
Trigger: charging_inductor(source_voltage=0, resistance=10, inductance=20, time=5) or any negative source_voltage. Note the parameter is named time (not time_sec).
Common situations: Unset config defaults of 0; swapping the positional order of source_voltage and time; feeding signed AC samples into a DC step-response model.
Related errors
- Capacitor at index {index} has a negative or zero value!
- You cannot supply more or less than 2 values
- Source voltage must be positive.
- Resistance must be positive.
- Inductance must be positive.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/3fbd3bf8979ac05c.
Report an issue: GitHub.