# # MathPoc : Proof of concept of a simple math problem, bringing it to the browser # # Alex Boschmans # # Version 0.2, February 2011 # # 0.2 Added some error checks and expanded math to not just adding but also # subtraction and multiplication and divisions. Extensively commented code. header = """
Please answer the following question
How much is %d %s %d ?
""" def generatequestion(): # This generates the question that we will pose using the random function # Generate a random question using 2 random numbers between 1 and 10 number1 = random.randint(1,10) number2 = random.randint(1,10) # Now we choose an operatioin ops = ["+", "-", "x", "/"] operation = random.choice(ops) # Let's check the division if operation == "/": # Prevent divisions with remainders using the modulo operator # Using module on the two numbers evaluates to 0 when no remainder is present # While the modulo remainder is not equal to 0, generate two new numbers while number1 % number2 <> 0: number1 = random.randint(1,10) number2 = random.randint(1,10) # Assemble the html, inputting the numbers in the foreseen places in the html # In a more extensive project, you would keep this html in a template file and # call it with a dictionary of items that need to be filled in the template question = indexhtml % (number1, operation, number2, number1, number2, operation) # Add common html like header and footer - these are defined just once and reused # for each page html = header + question + footer # Return the completed html to the calling function (in this case index) return html # This is the class that the cherrypy server uses and where you create the views that the # webuser sees. After each definition there is aThe question was : %s %s %s = ?
Your answer %s is wrong. The correct answer is %d.
""" % (number1, operation, number2, answer, solution) else: html = """The question was : %s %s %s = %s
Your answer is correct !
""" % (number1, operation, number2, answer) else: # We did not receive an answer html = """You need to fill in an answer !
""" # Return the page to the user, adding the common html return header + html + footer response.exposed = True if __name__ == '__main__': import random import cherrypy import os, sys # Set the current directory - this is probably not needed for this example, cruft. try: current_dir = os.path.dirname(os.path.abspath(__file__)) except: # probably running inside py2exe which doesn't set __file__ current_dir = os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding( ))) # Set up site-wide config first so we get a log if errors occur. # Adding the setting 'environment': 'production' to the below turns off auto-reload. # Otherwise CherryPy monitors the code and any change to code reloads the server - handy for development ! cherrypy.config.update({'server.socket_port':8888, 'server.socket_host':'127.0.0.1', 'log.error_file': 'site.log', 'log.screen': True}) # CherryPy will complain of an empty config but will continue conf = {} cherrypy.tree.mount(MathPoc()) #cherrypy.config.update({'server.socket_port':8888}) cherrypy.quickstart(MathPoc(),'/', config=conf)