Two resistor voltage output

For zleap!
This commit is contained in:
Mark 2022-04-25 18:25:20 +01:00
parent ee77cf10b9
commit ef9ac46106
1 changed files with 43 additions and 0 deletions

View File

@ -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))