{"record":{"id":"b4c59f4f14f630a6","repo":"TheAlgorithms/Python","slug":"source-voltage-must-be-positive","errorCode":null,"errorMessage":"Source voltage must be positive.","messagePattern":"Source voltage must be positive\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/charging_capacitor.py","lineNumber":61,"sourceCode":"\n    >>> charging_capacitor(source_voltage=0,resistance=10.0,capacitance=.30,time_sec=3)\n    Traceback (most recent call last):\n        ...\n    ValueError: Source voltage must be positive.\n\n    >>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4)\n    Traceback (most recent call last):\n        ...\n    ValueError: Resistance must be positive.\n\n    >>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4)\n    Traceback (most recent call last):\n        ...\n    ValueError: Capacitance must be positive.\n    \"\"\"\n\n    if source_voltage <= 0:\n        raise ValueError(\"Source voltage must be positive.\")\n    if resistance <= 0:\n        raise ValueError(\"Resistance must be positive.\")\n    if capacitance <= 0:\n        raise ValueError(\"Capacitance must be positive.\")\n    return round(source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":43,"sourceCodeEnd":73,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/charging_capacitor.py#L43-L73","documentation":"Thrown by charging_capacitor() in electronics/charging_capacitor.py when source_voltage <= 0. The function models a step-response charge curve V(t) = Vs*(1 - e^(-t/RC)) and requires a strictly positive driving voltage; a non-positive source makes the model (and its 'charging' semantics) invalid.","triggerScenarios":"charging_capacitor(source_voltage=0, resistance=1500, capacitance=30, time_sec=4) or any call with a negative source_voltage.","commonSituations":"Default-initializing voltage to 0 and forgetting to set it; AC or bipolar sources where a signed instantaneous value can be <= 0; parameter-order mix-ups passing time in the voltage slot.","solutions":["Pass a strictly positive source voltage (e.g. 5.0 or 20.0).","Use abs() only if you deliberately want magnitude of a bipolar source — otherwise fix the sign upstream.","Check that you are not passing defaults from an unset config field (0 is the classic default)."],"exampleFix":"# before\ncharging_capacitor(source_voltage=-9, resistance=1500, capacitance=30, time_sec=4)\n\n# after\ncharging_capacitor(source_voltage=9, resistance=1500, capacitance=30, time_sec=4)","handlingStrategy":"validation","validationCode":"if source_voltage <= 0:\n    raise ValueError(\"source_voltage must be positive\")\nv = charging_capacitor(source_voltage, resistance, capacitance, time_sec)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fail on unset config fields instead of letting 0 defaults flow into physics calls.","Use keyword arguments for the four parameters."],"tags":["electronics","rc-circuit","physics","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}