separate functions
This commit is contained in:
@@ -20,9 +20,19 @@ def find_bests(desired_ratio, resistors):
|
||||
for i, (error, R1, R2, ratio) in enumerate(candidates, start=1):
|
||||
print(f"{i}. R1 = {R1} Ω, R2 = {R2} Ω -> ratio = {ratio:.6f}, error = {error:.6e}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
raw_input = input("Enter resistor values (floats) separated by spaces or commas: ")
|
||||
resistors = [float(x) for x in raw_input.replace(',', ' ').split() if x.strip()]
|
||||
def parse_resistors(raw_input):
|
||||
values = []
|
||||
for token in raw_input.replace(',', ' ').split():
|
||||
try:
|
||||
v = float(token)
|
||||
if v <= 0:
|
||||
raise ValueError
|
||||
values.append(v)
|
||||
except ValueError:
|
||||
raise ValueError(f"Invalid resistor value '{token}'.")
|
||||
return values
|
||||
|
||||
def get_ratio():
|
||||
while True:
|
||||
try:
|
||||
desired_ratio = float(input("Enter desired ratio (0<ratio<=1): "))
|
||||
@@ -32,6 +42,20 @@ if __name__ == '__main__':
|
||||
print("Invalid ratio.")
|
||||
except ValueError:
|
||||
print("Invalid input.")
|
||||
return desired_ratio
|
||||
|
||||
def get_inputs():
|
||||
raw_input_resistors = input("Enter resistor values (floats) separated by spaces or commas: ")
|
||||
resistors = parse_resistors(raw_input_resistors)
|
||||
desired_ratio = get_ratio()
|
||||
|
||||
return (resistors, desired_ratio)
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
resistors, desired_ratio = get_inputs()
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
exit(1)
|
||||
find_bests(desired_ratio, resistors)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user