Solve via WolframAlpha (Python 3.3.x) - Printable Version

+- Sinisterly (
+-- Forum: Coding (
+--- Forum: Python (
+--- Thread: Solve via WolframAlpha (Python 3.3.x) (/Thread-Solve-via-WolframAlpha-Python-3-3-x)

Solve via WolframAlpha (Python 3.3.x) - Shebang - 06-10-2014

Required imports:
from urllib import parse, request
from html.parser import HTMLParser

def wolfram(problem):
    aurl = "" + parse.quote_plus(problem)
    data = str(request.urlopen(aurl).read(), encoding="utf-8")
    return wolfram_fix(data.split("\"stringified\": \"")[2].split("\"")[0])
def wolfram_fix(data):
    return HTMLParser().unescape(data).replace("\\/", "/").replace("pi", u"\u03c0")

Example Usage:
def main():
    while True:
        problem = input("Enter a problem to solve: ")

Example Output:
Enter a problem to solve: solve 5x+3=3 for x
x = 0
Enter a problem to solve: solve 8x^2+5=10 for x
x = ±sqrt(5/2)/2 ~~ ±0.79057
Enter a problem to solve: solve 5*x=pi for x
x = π/5 ~~ 0.62832

Revision 0: Initial release.
Revision 1: Added support for the 'pi' character.

RE: Solve via WolframAlpha (Python 3.3.x) - Null_Byte - 08-26-2014

Nice tutorial Biggrin Honestly didn't think of this before Tongue