parent
ee77cf10b9
commit
ef9ac46106
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
msg = """Voltage Divider Calculator (v1.1)
|
||||
Formula: Voltage out Voltage in * Resistor 2 / Resistor 1 + Resistor 2"
|
||||
|
||||
You entered:
|
||||
Voltage in {voltage}
|
||||
Resistor 1 {resistor1}
|
||||
Resistor 2 {resistor2}
|
||||
|
||||
Which equals:
|
||||
{output}
|
||||
|
||||
Output voltage is: "{output}", rounded (nearest 10) is "{rounded}"!
|
||||
"""
|
||||
|
||||
error = """Usage: python3 {script} <voltage in> <resistor 1> <resister 2>.
|
||||
Example: python3 {script} 5000 2000 4000
|
||||
|
||||
Seeing an Error?
|
||||
ValueError: You enter an invalid value (or left it empty).
|
||||
"""
|
||||
|
||||
|
||||
def main(args):
|
||||
script = args.pop(0)
|
||||
|
||||
try:
|
||||
voltage, resistor1, resistor2 = list(map(int, args))
|
||||
output = voltage * (resistor2 / (resistor1 + resistor2))
|
||||
except ValueError:
|
||||
print(error.format(script=script))
|
||||
raise
|
||||
|
||||
print(msg.format(voltage=voltage, resistor1=resistor1,
|
||||
resistor2=resistor2, output=output,
|
||||
rounded=round(output)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
sys.exit(main(sys.argv))
|
Loading…
Reference in New Issue