There are various ways to solve Wordy.
Using eval
is a convenient but potentially dangerous approach.
Another approach could replace the operation words with dunder methods.
They are called "dunder" methods because they have **d**ouble **under**scores at the beginning and end of the method name.
They are also called magic methods.
The dunder methods can be called by using the __getattribute__
method for int
.
Parsing should verify that the expression in words can be translated to a valid mathematical expression.
OPS = {
"plus": "__add__",
"minus": "__sub__",
"multiplied by": "__mul__",
"divided by": "__truediv__"
}
def answer(question):
question = question.removeprefix("What is").removesuffix("?").strip()
if not question: raise ValueError("syntax error")
if question.isdigit(): return int(question)
found_op = False
for name, op in OPS.items():
if name in question:
question = question.replace(name, op)
found_op = True
if not found_op: raise ValueError("unknown operation")
ret = question.split()
while len(ret) > 1:
try:
x, op, y, *tail = ret
if op not in OPS.values(): raise ValueError("syntax error")
ret = [int(x).__getattribute__(op)(int(y)), *tail]
except:
raise ValueError("syntax error")
return ret[0]
For more information, check the dunder method with __getattribute__
approach.