SLP assignment: your assignment is about classes & methods, inheritance,input/output, linked lists, and improve the program questor.
Require
This is a Python program which has a class and other features. In this script, you think of an item, and the computer tries to guess it. When it is wrong, you teach it about your new item. After a few dozen games, it starts to get pretty smart!
# questor.py # define some constants for future usekQuestion = ‘question’kGuess = ‘guess’# define a function for asking yes/no questionsdef yesno(prompt):ans = raw_input(prompt)return (ans[0]==’y’ or ans[0]==’Y’)# define a node in the question tree (either question or guess)class Qnode:# initialization methoddef __init__(self,guess):self.nodetype = kGuessself.desc = guess# get the question to ask def query(self):if (self.nodetype == kQuestion):return self.desc + ” “elif (self.nodetype == kGuess):return “Is it a ” + self.desc + “? “else:return “Error: invalid node type!”# return new node, given a boolean responsedef nextnode(self,answer):return self.nodes[answer]# turn a guess node into a question node and add new item# give a question, the new item, and the answer for that itemdef makeQuest( self, question, newitem, newanswer ):# create new nodes for the new answer and old answernewAnsNode = Qnode(newitem)oldAnsNode = Qnode(self.desc)# turn this node into a question nodeself.nodetype = kQuestionself.desc = question# assign the yes and no nodes appropriatelyself.nodes = {newanswer:newAnsNode, not newanswer:oldAnsNode}def traverse(fromNode):# ask the questionyes = yesno( fromNode.query() )# if this is a guess node, then did we get it right?if (fromNode.nodetype == kGuess):if (yes):print “I’m a genius!!!”return# if we didn’t get it right, return the nodereturn fromNode# if it’s a question node, then ask another questionreturn traverse( fromNode.nextnode(yes) )def run():# start with a single guess nodetopNode = Qnode(‘python’)done = 0while not done:# ask questions till we get to the endresult = traverse( topNode )# if result is a node, we need to add a questionif (result):item = raw_input(“OK, what were you thinking of? “)print “Enter a question that distinguishes a”,print item, “from a”, result.desc + “:”q = raw_input()ans = yesno(“What is the answer for ” + item + “? “)result.makeQuest( q, item, ans )print “Got it.”# repeat until doneprintdone = not yesno(“Do another? “)print# immediate-mode commands, for drag-and-drop or execfile() executionif __name__ == ‘__main__’:run()printraw_input(“press Return>”)else:print “Module questor imported.”print “To run, type: questor.run()”print “To reload after changes to the source, type: reload(questor)”# end of questor.py
Add the Python files from all 3 requirements above to one zip file. Add a one page write-up of your results and experiences to the zip file.
SLP assignment expectations:
Upload the zip file to coursenet.