Reputation: 323
So I am working on an assistant where the user will ask for an answer for a math problem and wolfram alpha will return it.
My problem right now is: Wolfram alpha returns the next step of the math problem.
What I want: Wolfram alpha should return the answer in decimal or integer after solving the whole problem.
My code:
import wolframalpha
query = input("calculate: ")
client = wolframalpha.Client("TYXXXX-PXXXXXXXX2")
query = query.replace(" ", "")
res = client.query(' '.join(query))
answer = next(res.results).text
print("calculating..")
print(f"The answer is {answer}")
Example:
input: 2x = 5
The answer I am looking for:
output: 2.5
The answer I get right now: output: 5/2
input: 2x = 5 - 3
The answer I am looking for: output: x = 1
The answer I get: 2x = 2
Upvotes: 0
Views: 1788
Reputation: 15384
In this way you can have the final solution provided by WolframAlpha (you just need to get the last element of the generator, instead of retrieving only the first one):
import wolframalpha
client = wolframalpha.Client(AppID)
response = client.query(input())
# Get the last item of the generator res.results
result = None
for result in response.results:
pass
# You could have also simply used result = list(response.results)[-1]
if result is not None:
print(f"The answer is {result.text}".format(result.text))
If you plan to use this program with simple linear equations using only the x variable then you could do something like this:
print("The answer is {}".format(eval(result.text.replace("x = ", ""))))
However, this would not work with inputs such as 2x=y+3
.
Possible inputs include equations such as:
2x=5
x-3=2x+2
etc.
Upvotes: 1