forked from bellshade/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ohm.py
38 lines (35 loc) · 1.33 KB
/
ohm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def ohm(voltage: float, current: float, resistance: float) -> dict[str, float]:
"""
Terapkan Hukum Ohm, pada dua nilai listrik yang diberikan,
yang dapat berupa tegangan, arus,
dan resistensi, dan kemudian dalam Python dict return nama/nilai dari nilai nol.
>>> ohm(voltage=10, resistance=5, current=0)
{'current': 2.0}
>>> ohm(voltage=0, current=0, resistance=10)
Traceback (most recent call last):
...
ValueError: Hanya satu argumen 0, yang bisa diisi.
>>> ohm(voltage=0, current=1, resistance=-2)
Traceback (most recent call last):
...
ValueError: Resistan tidak boleh negatif.
>>> ohm(resistance=0, voltage=-10, current=1)
{'resistance': -10.0}
>>> ohm(voltage=0, current=-1.5, resistance=2)
{'voltage': -3.0}
"""
if (voltage, current, resistance).count(0) != 1:
raise ValueError("Hanya satu argumen 0, yang bisa diisi.")
if resistance < 0:
raise ValueError("Resistan tidak boleh negatif.")
if voltage == 0:
return {"voltage": float(current * resistance)}
elif current == 0:
return {"current": voltage / resistance}
elif resistance == 0:
return {"resistance": voltage / current}
else:
raise ValueError("Exactly one argument must be 0")
if __name__ == "__main__":
import doctest
doctest.testmod()