TheAlgorithms/Python · error · ValueError
Resistance must be positive.
Error message
Resistance must be positive.
What it means
Thrown by charging_capacitor() when resistance <= 0. The RC time constant tau = R*C appears in the exponent of the charging curve; zero or negative resistance makes the model undefined (instant/inverted charging), so the library rejects it.
Source
Thrown at electronics/charging_capacitor.py:63
Traceback (most recent call last):
...
ValueError: Source voltage must be positive.
>>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4)
Traceback (most recent call last):
...
ValueError: Resistance must be positive.
>>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4)
Traceback (most recent call last):
...
ValueError: Capacitance must be positive.
"""
if source_voltage <= 0:
raise ValueError("Source voltage must be positive.")
if resistance <= 0:
raise ValueError("Resistance must be positive.")
if capacitance <= 0:
raise ValueError("Capacitance must be positive.")
return round(source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3)
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Use a strictly positive resistance, e.g. the actual series resistor value in ohms.
- If you assumed an ideal circuit with no resistor, this model cannot represent it — add a realistic ESR/series resistance.
- Verify keyword arguments; positional calls easily swap resistance and capacitance.
Example fix
# before charging_capacitor(source_voltage=20, resistance=-2000, capacitance=30, time_sec=4) # after charging_capacitor(source_voltage=20, resistance=2000, capacitance=30, time_sec=4)
Defensive patterns
Strategy: validation
Validate before calling
if resistance <= 0:
raise ValueError("resistance must be positive")
v = charging_capacitor(source_voltage, resistance, capacitance, time_sec) Prevention
- Never assume R=0; include ESR or winding resistance.
- Double-check ohm-unit conversions before the call.
When it happens
Trigger: charging_capacitor(source_voltage=20, resistance=-2000, capacitance=30, time_sec=4) or resistance=0. This check runs after the source-voltage check.
Common situations: Ideal-wire assumptions (R=0) carried over from schematic simplifications; unit confusion (kOhm vs Ohm) leading to negative/zero computed values; parameter-order mix-ups.
Related errors
- Electron concentration cannot be negative in a semiconductor
- Hole concentration cannot be negative in a semiconductor
- Intrinsic concentration cannot be negative in a semiconducto
- Source voltage must be positive.
- Capacitance must be positive.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/e4c1e54d13666bdf.
Report an issue: GitHub.