Skip to content

Commit 250fcd3

Browse files
authored
Create ohms_law.py
1 parent 40f65e8 commit 250fcd3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

physics/ohms_law.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def ohms_law(current: float, resistance: float) -> float:
2+
"""
3+
Calculate the voltage when current and resistance data is given using Ohm's Law.
4+
5+
Ohm's Law states that:
6+
V = I * R
7+
8+
V = Voltage (Volts)
9+
I = Current (Amperes)
10+
R = Resistance (Ohms)
11+
12+
>>> ohms_law(2,5)
13+
10.0
14+
15+
>>> ohms_law(1,4)
16+
4.0
17+
18+
>>> ohms_law(3,0.5)
19+
1.5
20+
21+
>>> ohms_law(1.5,2)
22+
3.0
23+
24+
# Test case for invalid input
25+
>>> ohms_law(2,0)
26+
Traceback (most recent call last):
27+
...
28+
ValueError: Zero resistance
29+
30+
>>> ohms_law(0,3)
31+
Traceback (most recent call last):
32+
...
33+
ValueError: Zero current
34+
"""
35+
if current or resistance ==0
36+
raise ValueError('Current or Resistance cannot be zero')
37+
38+
voltage = current * resistance
39+
return voltage
40+
41+
42+
if __name__ == "__main__":
43+
import doctest
44+
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)