Sinisterly

Full Version: Solve via WolframAlpha (Python 3.3.x)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Nice tutorial Biggrin Honestly didn't think of this before Tongue