Skip to content

Commit bd8a957

Browse files
committed
charging_capacitor_algo
2 parents f07eefd + fa89e7b commit bd8a957

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Diff for: electronics/charging_capacitor.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def charging_capacitor(
2424
resistance: float, # resistance in ohms.
2525
capacitance: float, # capacitance in farads.
2626
time_sec: float, # time in seconds after charging initiation of capacitor.
27-
) -> float:
27+
):
2828
"""
2929
find voltage of capacitor at any nth second after the initiation of it's charging.
3030
@@ -83,21 +83,29 @@ def charging_capacitor(
8383
raise ValueError("source voltage cannot be negative.")
8484
elif source_voltage == 0:
8585
raise ValueError("source voltage cannot be zero.")
86+
else:
87+
return None
8688
elif resistance <= 0:
8789
if resistance < 0:
8890
raise ValueError("Resistance cannot be negative.")
8991
elif resistance == 0:
9092
raise ValueError("Resistance cannot be zero.")
93+
else:
94+
return None
9195
elif capacitance <= 0:
9296
if capacitance < 0:
9397
raise ValueError("Capacitance cannot be negative.")
9498
elif capacitance == 0:
9599
raise ValueError("Capacitance cannot be zero.")
100+
else:
101+
return None
96102
else:
97-
return round(
98-
source_voltage * (1 - exp(-time_sec / (resistance * capacitance))),
99-
3,
100-
)
103+
if resistance != 0:
104+
return round(
105+
source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3
106+
)
107+
else:
108+
return None
101109

102110

103111
if __name__ == "__main__":

0 commit comments

Comments
 (0)