By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Cpickle/Data file need program to retain information

P: 1
I have a program and I want the program to be able to save question/answers so the program actually gets smarter without getting amnesia everytime its started. Can anyone help???

Expand|Select|Wrap|Line Numbers
  1. # questor.py
  2.  
  3.  # define some constants for futureuse
  4.  
  5. kQuestion = 'question'
  6. kGuess = 'guess'
  7.  
  8. # define a function for asking yes/no questions
  9. def yesno(prompt): 
  10.           ans = raw_input(prompt) 
  11.           return (ans[0]=='y' or ans[0]=='Y')
  12.  
  13. # define a node in the question tree (either question or guess)
  14. class Qnode: 
  15.  
  16.           # initialization method 
  17.           def __init__(self,guess): 
  18.                      self.nodetype = kGuess 
  19.                      self.desc = guess 
  20.           # get the question to ask 
  21.           def query(self): 
  22.                      if (self.nodetype == kQuestion): 
  23.                                return self.desc + " " 
  24.                      elif (self.nodetype == kGuess): 
  25.                         return "Is it a " + self.desc + "? " 
  26.                      else: 
  27.                         return "Error: invalid node type!" 
  28.  
  29.           # return new node, given a boolean response 
  30.           def nextnode(self,answer): 
  31.                      return self.nodes[answer] 
  32.  
  33.           # turn a guess node into a question node and add new item 
  34.        # give a question, the new item, and the answer for that item   
  35.           def makeQuest( self, question, newitem, newanswer ): 
  36.  
  37.        # create new nodes for the new answer and old answer newAnsNode = Qnode(newitem) 
  38. oldAnsNode = Qnode(self.desc) 
  39.  
  40. # turn this node into a question node 
  41. self.nodetype = kQuestion 
  42. self.desc = question 
  43.  
  44. # assign the yes and no nodes appropriately 
  45. self.nodes = {newanswer:newAnsNode, not newanswerdAnsNode} 
  46.  
  47. def traverse(fromNode): 
  48.            # ask the question 
  49.            yes = yesno( fromNode.query() ) 
  50.  
  51.            # if this is a guess node, then did we get it right? 
  52.            if (fromNode.nodetype == kGuess):
  53.                       if (yes): 
  54.                          print "I'm a genius!!!" 
  55.                          return 
  56.            # if we didn't get it right, return the node 
  57.            return fromNode 
  58.  
  59.            # if it's a question node, then ask another question 
  60.            return traverse( fromNode.nextnode(yes) )
  61.  
  62. def run(): 
  63.            # start with a single guess node 
  64.            topNode = Qnode('python') 
  65.  
  66.            done = 0 
  67.            while not done: 
  68.                       # ask questions till we get to the end 
  69.                       result = traverse( topNode ) 
  70.  
  71.                       # if result is a node, we need to add a question 
  72.                       if (result):
  73.                  item = raw_input("OK, what were you thinking of? ") 
  74.                  print "Enter a question that distinguishes a", 
  75.                  print item, "from a", result.desc + ":" 
  76.                  q = raw_input() 
  77.                 ans = yesno("What is the answer for " + item + "?")          
  78.                 result.makeQuest( q, item, ans ) 
  79.                 print "Got it." 
  80.  
  81.                        # repeat until done
  82.                        print 
  83.                        done = not yesno("Do another? ") 
  84.                        print
  85.  
  86.  
  87.  
  88. #  immediate-mode commands, for drag-and-drop or execfile() execution
  89. if __name__ == '__main__': 
  90.           run() 
  91.           print 
  92.           raw_input("press Return>")
  93. else: 
  94.  
  95.           print "Module questor imported." 
  96.           print "To run, type: questor.run()" 
  97.           print "To reload after changes to the source, type: reload(questor)"
  98. # end of questor.py
Aug 10 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.