Added should_calc check and done expression var
This commit is contained in:
9
calc.py
9
calc.py
@@ -12,6 +12,7 @@ def calc(text):
|
|||||||
returns (expression, result) or None
|
returns (expression, result) or None
|
||||||
"""
|
"""
|
||||||
expression = ""
|
expression = ""
|
||||||
|
d_expression = ""
|
||||||
should_calc = False
|
should_calc = False
|
||||||
for char in text:
|
for char in text:
|
||||||
if char.isdigit() or (should_calc and char in [".", " "]):
|
if char.isdigit() or (should_calc and char in [".", " "]):
|
||||||
@@ -22,15 +23,17 @@ def calc(text):
|
|||||||
expression += char
|
expression += char
|
||||||
elif char.isalpha():
|
elif char.isalpha():
|
||||||
# don't include any more text in the calculation
|
# don't include any more text in the calculation
|
||||||
|
if should_calc:
|
||||||
|
d_expression = expression
|
||||||
expression = ""
|
expression = ""
|
||||||
if should_calc and not any(op in expression for op in ignore_operators):
|
if should_calc and not any(op in d_expression for op in ignore_operators):
|
||||||
try:
|
try:
|
||||||
result = str(eval(expression)) # pylint: disable = W0123
|
result = str(eval(d_expression)) # pylint: disable = W0123
|
||||||
except: # pylint: disable = W0702
|
except: # pylint: disable = W0702
|
||||||
# we can run into all kinds of errors here
|
# we can run into all kinds of errors here
|
||||||
# most probably SyntaxError
|
# most probably SyntaxError
|
||||||
return None
|
return None
|
||||||
return (expression, result)
|
return (d_expression, result)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user