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
46 changes: 46 additions & 0 deletions electronics/ohms_law.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
def ohms_law(voltage: float, current: float, resistance: float) -> float:
"""
This function apply ohm's law, on any two given electrical values,
which can be voltage current resistance,
and then return name and value pair in the form of dictionary
>>> ohms_law(voltage=10, resistance=5, current=0)
{'current': 2.0}
>>> ohms_law(voltage=0, current=0, resistance=10)
Traceback (most recent call last):
File "<stdin>", line 14, in <module>
ValueError: Only one argument can be 0 at the time
>>> ohms_law(resistance=0, voltage=-10, current=1)
{'resistance': -10.0}
>>> ohms_law(voltage=0, current=-1.5, resistance=2)
{'voltage': -3.0}
"""
if voltage == 0:
if current == 0 or resistance == 0:
raise ValueError("Only one argument can be 0 at the time")
elif resistance <= 0:
raise ValueError("Resistance can't be 0 or in negative")
else:
result = {"voltage": float(current * resistance)}
return result
Copy link
Member

@cclauss cclauss Nov 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply remove variables that are created on one line and then deleted on the next line.

Suggested change
result = {"voltage": float(current * resistance)}
return result
return {"voltage": float(current * resistance)}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we cast as float() on this branch but not on the other two branches?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if you divide two values its gives you float but not in case for the multiplication , and remember i give return type float in type hint

elif current == 0:
if voltage == 0 or resistance == 0:
raise ValueError("Only one argument can be 0 at the time")
elif resistance <= 0:
raise ValueError("Resistance can't be 0 or in negative")
else:
result = {"current": voltage / resistance}
return result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result = {"current": voltage / resistance}
return result
return {"current": voltage / resistance}

elif resistance == 0:
if voltage == 0 or current == 0:
raise ValueError("Only one argument can be 0 at the time")
else:
result = {"resistance": voltage / current}
return result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result = {"resistance": voltage / current}
return result
return {"resistance": voltage / current}



if __name__ == "__main__":
# Importing doctest to test our function
from doctest import testmod

# Tesmod function is called to run test
testmod(name="ohms_law", verbose=True)