4242valid_variables : list [str ] = ["v1" , "v2" , "p1" , "p2" ]
4343
4444
45- def check_validity (values : dict [str , float ]) -> bool :
45+ def check_validity (values : dict [str , float ]):
4646 """
4747
4848 Function takes dictionary as an input and returns True if the input
@@ -51,26 +51,25 @@ def check_validity(values: dict[str, float]) -> bool:
5151 >>> check_validity({})
5252 Traceback (most recent call last):
5353 ...
54- ValueError: Invalid input expected 3 items got 0
54+ ValueError: Invalid input expected 3 items, got 0
5555
5656 >>> check_validity({'v1':2,'v2':4,'k':6})
5757 Traceback (most recent call last):
5858 ...
5959 ValueError: Invalid input k is not a valid variable
6060
6161 >>> check_validity({'v1':2,'v2':4,'p1':6})
62- True
6362
6463 """
65- if len (values ) == 3 :
64+ if len (values ) != 3 :
65+ msg = f"Invalid input expected { 3 } items, got { len (values )} "
66+ raise ValueError (msg )
67+ else :
6668 for value in values :
6769 if value not in valid_variables :
6870 msg = f"Invalid input { value } is not a valid variable"
6971 raise ValueError (msg )
70- return True
71- else :
72- msg = f"Invalid input expected { 3 } items got { len (values )} "
73- raise ValueError (msg )
72+ return
7473
7574
7675def find_target_variable (values : dict [str , float ]) -> str :
@@ -83,7 +82,7 @@ def find_target_variable(values: dict[str, float]) -> str:
8382 >>> find_target_variable({})
8483 Traceback (most recent call last):
8584 ...
86- ValueError: Invalid input expected 3 items got 0
85+ ValueError: Invalid input expected 3 items, got 0
8786
8887 >>> find_target_variable({'v1':1,'v2':2,'p2':4})
8988 'p1'
@@ -94,14 +93,11 @@ def find_target_variable(values: dict[str, float]) -> str:
9493 ValueError: Invalid input k is not a valid variable
9594
9695 """
97- is_valid = check_validity (values )
98- if is_valid :
99- for variable in valid_variables :
100- if variable not in values :
101- return variable
102- raise ValueError ("Input is invalid" )
103- else :
104- raise ValueError ("Input is invalid" )
96+ check_validity (values )
97+ for variable in valid_variables :
98+ if variable not in values :
99+ return variable
100+ raise ValueError ("Input is invalid" )
105101
106102
107103def boyles_law (values : dict [str , float ]) -> dict [str , str ]:
@@ -115,12 +111,12 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]:
115111 >>> boyles_law({'p1':2,'v2':1})
116112 Traceback (most recent call last):
117113 ...
118- ValueError: Invalid input expected 3 items got 2
114+ ValueError: Invalid input expected 3 items, got 2
119115
120116 >>> boyles_law({})
121117 Traceback (most recent call last):
122118 ...
123- ValueError: Invalid input expected 3 items got 0
119+ ValueError: Invalid input expected 3 items, got 0
124120
125121 >>> boyles_law({'p1':2,'v2':1, 'k':6})
126122 Traceback (most recent call last):
@@ -140,32 +136,29 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]:
140136 {'v1': '31.32 L'}
141137
142138 """
143- is_valid = check_validity (values )
144- if is_valid :
145- target = find_target_variable (values )
146- float_precision = ".3f"
147- if target == "p1" :
148- p1 = float (
149- format ((values ["p2" ] * values ["v2" ]) / values ["v1" ], float_precision )
150- )
151- return {"p1" : f"{ p1 } Pa" }
152- elif target == "v1" :
153- v1 = float (
154- format ((values ["p2" ] * values ["v2" ]) / values ["p1" ], float_precision )
155- )
156- return {"v1" : f"{ v1 } L" }
157- elif target == "p2" :
158- p2 = float (
159- format ((values ["p1" ] * values ["v1" ]) / values ["v2" ], float_precision )
160- )
161- return {"p2" : f"{ p2 } Pa" }
162- else :
163- v2 = float (
164- format ((values ["p1" ] * values ["v1" ]) / values ["p2" ], float_precision )
165- )
166- return {"v2" : f"{ v2 } L" }
139+ check_validity (values )
140+ target = find_target_variable (values )
141+ float_precision = ".3f"
142+ if target == "p1" :
143+ p1 = float (
144+ format ((values ["p2" ] * values ["v2" ]) / values ["v1" ], float_precision )
145+ )
146+ return {"p1" : f"{ p1 } Pa" }
147+ elif target == "v1" :
148+ v1 = float (
149+ format ((values ["p2" ] * values ["v2" ]) / values ["p1" ], float_precision )
150+ )
151+ return {"v1" : f"{ v1 } L" }
152+ elif target == "p2" :
153+ p2 = float (
154+ format ((values ["p1" ] * values ["v1" ]) / values ["v2" ], float_precision )
155+ )
156+ return {"p2" : f"{ p2 } Pa" }
167157 else :
168- raise ValueError ("Input is invalid" )
158+ v2 = float (
159+ format ((values ["p1" ] * values ["v1" ]) / values ["p2" ], float_precision )
160+ )
161+ return {"v2" : f"{ v2 } L" }
169162
170163
171164if __name__ == "__main__" :
0 commit comments