Skip to content

Ohm's Law algorithm added #3934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Nov 25, 2020
Merged
Prev Previous commit
Next Next commit
update decimal value & negative value test
  • Loading branch information
erdum committed Nov 24, 2020
commit a120c71c345a4afb4d4c3f0c3f2c07655fc488fc
27 changes: 21 additions & 6 deletions electronics/ohms_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,32 @@ def ohms_law(voltage: float = 0, current: float = 0, resistance: float = 0) -> f
This function calculates the any one
of the three ohms_law values voltage,
current, resistance, of electronics.
>>> ohms_law(voltage=10, resistance=5,current=0 )
Note: "resistance cannot be negative"
>>> ohms_law(voltage=10, resistance=5)
2.0
>>> ohms_law(current=1, resistance=10, voltage=0)
>>> ohms_law(current=1, resistance=10)
10.0
>>> ohms_law(voltage=1.5, resistance=10)
0.15
>>> ohms_law(voltage=-15, resistance=10)
-1.5
>>> ohms_law(current=-1, resistance=2)
-2.0
>>> ohms_law(voltage=-15, resistance=-10)
0
"""
if voltage == 0:
result = float(current * resistance)
return result
if resistance > 0:
result = float(current * resistance)
return result
else:
return 0
elif current == 0:
result = voltage / resistance
return result
if resistance > 0:
result = voltage / resistance
return result
else:
return 0
elif resistance == 0:
result = voltage / current
return result
Expand Down