<div> </div> <DIV>I am new to python and computer programming. I am trying to save the questor.py file so that it retains it's data. My problem is that I don't really understand how to do it. I have been reviewing tutorials and I have gotten this far, but the data still doesn't save. Can someone tell me what it is I am overlooking?</DIV> <DIV> </DIV> <div># questor.py </div> <div># define some constants for future use</div> <div>kQuestion = 'question'<BR>kGuess = 'guess'</div> <div># define a function for asking yes/no questions<BR>def yesno(prompt):<BR> ans = raw_input(prompt)<BR> return (ans[0]=='y' or ans[0]=='Y')</div> <div># define a node in the question tree (either question or guess)<BR>class Qnode:<BR> <BR> # initialization method<BR> def __init__(self,guess):<BR> self.nodetype = kGuess<BR> self.desc = guess<BR> <BR> def retrieve(self, file):<BR> self.desc =
file.readline().rstrip()<BR> self.nodetype = file.readline().rstrip()<BR> if self.nodetype == kQuestion:<BR> slef.nodes = {0:Qnode(''), 1:Qnode('')}<BR> retrieve(self.nodes[0], file)<BR> retrieve(self.nodes[1], file)<BR> return<BR> <BR> def save(self, file):<BR> file.write(self.desc + "\n")<BR> file.write(self.nodetype + "\n")<BR> if self.nodetype == kQuestion:<BR> save(self.nodes[0], file)<BR> save(self.nodes[1], file)<BR> return<BR> # get the question to ask <BR> def query(self):<BR> if (self.nodetype == kQuestion):<BR> return self.desc + " "<BR> elif (self.nodetype == kGuess):<BR> return "Is it a " + self.desc + "? "<BR> else:<BR> return "Error: invalid node type!"</div> <div> # return new node, given
a boolean response<BR> def nextnode(self,answer):<BR> return self.nodes[answer]</div> <div> # turn a guess node into a question node and add new item<BR> # give a question, the new item, and the answer for that item<BR> def makeQuest( self, question, newitem, newanswer ):</div> <div> # create new nodes for the new answer and old answer<BR> newAnsNode = Qnode(newitem)<BR> oldAnsNode = Qnode(self.desc)</div> <div> # turn this node into a question node<BR> self.nodetype = kQuestion<BR> self.desc = question</div> <div> # assign the yes and no nodes appropriately<BR> self.nodes = {newanswer:newAnsNode, not newanswer:oldAnsNode}<BR> <BR> </div> <div>def traverse(fromNode):<BR> # ask the question<BR> yes = yesno( fromNode.query() )<BR> <BR> # if this is a guess node, then did we get it right?<BR> if (fromNode.nodetype ==
kGuess):<BR> if (yes):<BR> print "I'm a genius!!!"<BR> return<BR> # if we didn't get it right, return the node<BR> return fromNode<BR> <BR> # if it's a question node, then ask another question<BR> return traverse( fromNode.nextnode(yes) )</div> <div>def run():<BR> # start with a single guess node<BR> try:<BR> f = open('c:\nodetree.txt', 'r')<BR> cont = f.readlines()<BR> known = cont.count('guess\n')<BR> del cont<BR> f.seek(0)<BR> topNode = Qnode('')<BR> retrieve(topNode, f)<BR> f.close()<BR> except:<BR> topNode = Qnode('python')<BR> known = 1<BR> <BR> <BR> <BR> done = 0<BR> while not done:<BR> # ask questions till we get to the end<BR> result = traverse( topNode )<BR> <BR> # if result is a node, we need to add
a question<BR> if (result):<BR> item = raw_input("OK, what were you thinking of? ")<BR> print "Enter a question that distinguishes a",<BR> print item, "from a", result.desc + ":"<BR> q = raw_input()<BR> ans = yesno("What is the answer for " + item + "? ")<BR> result.makeQuest( q, item, ans )<BR> print "Got it."<BR> <BR> # repeat until done<BR> print<BR> done = not yesno("Do another? ")<BR> print</div> <div><BR># immediate-mode commands, for drag-and-drop or execfile() execution<BR>if __name__ == '__main__':<BR> run()<BR> print<BR> raw_input("press Return>")<BR>else:<BR> print "Module questor imported."<BR> print "To run, type: questor.run()"<BR> print "To reload after changes to the source, type: reload(questor)"</div> <div># end of questor.py</div><p>
<hr size=1>Relax. Yahoo! Mail
<a href="http://us.rd.yahoo.com/mail_us/taglines/virusall/*http://communications.yahoo.com/features.php?page=221">virus scanning</a> helps detect nasty viruses!