TheAlgorithms/Python · error · ValueError
Resistance must be positive.
Error message
Resistance must be positive.
What it means
Thrown by charging_inductor() when resistance <= 0. Resistance sets the RL time constant L/R and the steady-state current Vs/R; zero or negative R makes the current infinite/undefined, so the library guards against it before computing.
Source
Thrown at electronics/charging_inductor.py:86
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
- Use the real series/winding resistance (even milliohms) instead of 0.
- If you truly need R=0, this step-response model does not apply — the current ramps linearly as V*t/L; compute that directly.
- Check unit conversions and keyword spelling.
Example fix
# before charging_inductor(source_voltage=10, resistance=0, inductance=20, time=5) # after charging_inductor(source_voltage=10, resistance=50, inductance=20, time=5)
Defensive patterns
Strategy: validation
Validate before calling
if resistance <= 0:
raise ValueError("resistance must be positive; use winding resistance")
i = charging_inductor(source_voltage, resistance, inductance, time) Prevention
- Real inductors have winding resistance — look up DCR in the datasheet.
- For true R=0, compute I = V*t/L yourself instead of calling this API.
When it happens
Trigger: charging_inductor(source_voltage=10, resistance=0, inductance=20, time=5) or a negative resistance value. Runs after the source-voltage check.
Common situations: Ideal (lossless) inductor assumptions of R=0 — physically real inductors always have winding resistance; unit mix-ups (kOhm vs Ohm); negative-resistance models (tunnel diodes) which this API does not support.
Related errors
- Source voltage must be positive.
- Inductance must be positive.
- Capacitor at index {index} has a negative or zero value!
- You cannot supply more or less than 2 values
- Electron concentration cannot be negative in a semiconductor
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/902d9f7db12270b4.
Report an issue: GitHub.