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

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/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:
Code:
from urllib import parse, request
from html.parser import HTMLParser

Functions:
Code:
def wolfram(problem):
    aurl = "http://www.wolframalpha.com/input/?i=" + 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:
Code:
def main():
    while True:
        problem = input("Enter a problem to solve: ")
        print(wolfram(problem))

Example Output:
Code:
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