-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Changes from 16 commits
3986d64
fa26af6
dd10b84
01cf2d3
79cc57c
75c0eea
71fa550
873b6eb
136887e
a120c71
b3d0f38
d36b7a0
0fd3f58
da97450
0af817c
f8bf2b4
e7327d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||||
def ohms_law(voltage: float, current: float, resistance: float) -> float: | ||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
""" | ||||||||
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} | ||||||||
""" | ||||||||
erdum marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if voltage == 0: | ||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we cast as There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
|
||||||||
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) |
Uh oh!
There was an error while loading. Please reload this page.