99typical order of operations.
1010"""
1111
12- STR_TO_OPERATOR : dict [str , str ] = {
13- "plus" : "+" ,
14- "minus" : "-" ,
15- "multiplied" : "*" ,
16- "divided" : "/" ,
17- }
18-
12+ OPERATORS : tuple = ("plus" , "minus" , "multiplied" , "divided" )
1913REPLACEABLE : tuple = ("What" , "is" , "by" )
2014
2115
@@ -74,14 +68,14 @@ def _math_operation(result: int, question: list[str]) -> int:
7468 math_operator : str = question [0 ]
7569 operand : int = int (question [1 ])
7670
77- match STR_TO_OPERATOR [ math_operator ] :
78- case "+ " :
71+ match math_operator :
72+ case "plus " :
7973 result += operand
80- case "- " :
74+ case "minus " :
8175 result -= operand
82- case "/ " :
76+ case "divided " :
8377 result //= operand
84- case "* " :
78+ case "multiplied " :
8579 result *= operand
8680
8781 return result
@@ -99,11 +93,11 @@ def _err_check(question: list[str]) -> None:
9993 """
10094 math_operator : str = question [0 ]
10195 # if the question contains an unknown operation.
102- if math_operator not in STR_TO_OPERATOR and not math_operator .isdigit ():
96+ if math_operator not in OPERATORS and not math_operator .isdigit ():
10397 raise ValueError ("unknown operation" )
10498 # if the question is malformed or invalid.
10599 if math_operator .isdigit ():
106100 raise ValueError ("syntax error" )
107101 # if the question is malformed or invalid.
108- if math_operator in STR_TO_OPERATOR and len (question ) == 1 :
102+ if math_operator in OPERATORS and len (question ) == 1 :
109103 raise ValueError ("syntax error" )
0 commit comments