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

Game won't Save

P: 2
I got my game to work but I when I run it and answer the first question it says Could not save.
Why is my game not saving.
It does create the save game file but it is always empty.
Please Help!!!



Expand|Select|Wrap|Line Numbers
  1. # questor.py 
  2.  
  3. # define some constants for future use
  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.  
  21. # get the question to ask 
  22.     def query(self):
  23.         if (self.nodetype == kQuestion):
  24.             return self.desc + " "
  25.         elif (self.nodetype == kGuess):
  26.             return "Is it a " + self.desc + "? "
  27.         else:
  28.             return "Error: invalid node type!"
  29.  
  30. # return new node, given a boolean response
  31.     def nextnode(self,answer):
  32.         return self.nodes[answer]
  33.  
  34. # turn a guess node into a question node and add new item
  35. # give a question, the new item, and the answer for that item
  36.     def makeQuest( self, question, newitem, newanswer ):
  37.  
  38. # create new nodes for the new answer and old answer
  39.         newAnsNode = Qnode(newitem)
  40.         oldAnsNode = Qnode(self.desc)
  41.  
  42. # turn this node into a question node
  43.         self.nodetype = kQuestion
  44.         self.desc = question
  45.  
  46. # assign the yes and no nodes appropriately
  47.         self.nodes = {newanswer:newAnsNode, not newanswer:oldAnsNode}
  48.  
  49.  
  50. def traverse(fromNode):
  51. # ask the question
  52.     yes = yesno( fromNode.query() )
  53.  
  54. # if this is a guess node, then did we get it right?
  55.     if (fromNode.nodetype == kGuess):
  56.         if (yes):
  57.             print "I'm a genius!!!"
  58.             return
  59. # if we didn't get it right, return the node
  60.         return fromNode
  61.  
  62. # if it's a question node, then ask another question
  63.     return traverse( fromNode.nextnode(yes) )
  64.  
  65. def run():
  66. # start with a single guess node
  67.     try:
  68.         f = file('questor.dat', 'r')
  69.         topNode = pickle.load(f)
  70.         f.close()
  71.     except:
  72.         topNode = Qnode('python')
  73.  
  74.     done = 0
  75.     while not done:
  76.          try:
  77.             f = file('questor.dat', 'w')
  78.             pickle.dump(topNode, f)
  79.             f.close()
  80.          except:
  81.             print "Could not save!"
  82. # ask questions till we get to the end
  83.          result = traverse( topNode )
  84.  
  85. # if result is a node, we need to add a question
  86.          if (result):
  87.             item = raw_input("OK, what were you thinking of? ")
  88.             print "Enter a question that distinguishes a",
  89.             print item, "from a", result.desc + ":"
  90.             q = raw_input()
  91.             ans = yesno("What is the answer for " + item + "? ")
  92.             result.makeQuest( q, item, ans )
  93.             print "Got it."
  94.  
  95. # repeat until done
  96.             print
  97.             done = not yesno("Do another? ")
  98.             #print
  99.  
  100. # immediate-mode commands, for drag-and-drop or execfile() execution
  101. if __name__ == '__main__':
  102.     run()
  103.     print
  104.     raw_input("press Return>")
  105. else:
  106.     print "Module questor imported."
  107.     print "To run, type: questor.run()"
  108.     print "To reload after changes to the source, type: reload(questor)"
  109.  
  110. # end of questor.py
Aug 28 '10 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 626
You can only pickle a class instance. Start herefor a class tutorial.
Aug 28 '10 #2

P: 1
@dwblas


The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure.
http://docs.python.org/library/pickle.html



also I ran your code you need to import pickle or cpickle and it will work
Sep 4 '11 #3

Post your reply

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