chevron_left chevron_right
Login Register invert_colors photo_library
Stay updated and chat with others! - Join the Discord!
• 0 Vote(s) - 0 Average

 Solve via WolframAlpha (Python 3.3.x) filter_list Linear Mode Threaded Mode View a Printable Version Author Message
Solve via WolframAlpha (Python 3.3.x) #1
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\": \"").split("\""))

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) #2
Nice tutorial Honestly didn't think of this before Users browsing this thread: 1 Guest(s)