@@ -61,18 +61,19 @@ def _math_operation(result: int, question: list[str]) -> tuple[int, list[str]]:
6161
6262 :param result: Accumulated left-hand value computed so far.
6363 :type result: int
64- :param question: Remaining tokens starting with the operator then rhs integer,
65- e.g. ``['plus', '4', ...]``.
64+ :param question: Remaining tokens starting with the operator then rhs
65+ integer, e.g. ``['plus', '4', ...]``.
6666 :type question: list[str]
67- :returns: A tuple of the new accumulated result and the remaining tokens after
68- consuming the operator and rhs.
67+ :returns: A tuple of the new accumulated result and the remaining tokens
68+ after consuming the operator and rhs.
6969 :rtype: tuple[int, list[str]]
70- :raises ValueError: If the operator is unknown or the token sequence is malformed.
70+ :raises ValueError: If the operator is unknown or the token sequence is
71+ malformed.
7172 """
7273 math_operator : str = question [0 ]
7374 # if the question contains an unknown operation.
7475 if (
75- math_operator not in STR_TO_OPERATOR . keys ()
76+ math_operator not in STR_TO_OPERATOR
7677 and not math_operator .isdigit ()
7778 ):
7879 raise ValueError ("unknown operation" )
@@ -84,13 +85,15 @@ def _math_operation(result: int, question: list[str]) -> tuple[int, list[str]]:
8485 raise ValueError ("syntax error" )
8586
8687 if STR_TO_OPERATOR [math_operator ] == "+" :
87- return result + int (question [1 ]), question [ 2 :]
88+ result += int (question [1 ])
8889
8990 if STR_TO_OPERATOR [math_operator ] == "-" :
90- return result - int (question [1 ]), question [ 2 :]
91+ result -= int (question [1 ])
9192
9293 if STR_TO_OPERATOR [math_operator ] == "/" :
93- return result // int (question [1 ]), question [ 2 :]
94+ result //= int (question [1 ])
9495
9596 if STR_TO_OPERATOR [math_operator ] == "*" :
96- return result * int (question [1 ]), question [2 :]
97+ result *= int (question [1 ])
98+
99+ return result , question [2 :]
0 commit comments