473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Game won't Save

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
2 2619
dwblas
626 Expert 512MB
You can only pickle a class instance. Start herefor a class tutorial.
Aug 28 '10 #2
@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

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

Similar topics

14
by: Schoo | last post by:
I have an asp.net app that uses session objects (ag. session("UserID")). The app works fine in development/debug mode. I released it to the test server (Windows 2000 server with other .NET...
0
by: AndyPC | last post by:
I'm currently developing this Class Library as a Plug-in, which has to maintain it's own mail list for the host application to receive. The Class library has a dialog in it (opened by the host app...
1
tpgames
by: tpgames | last post by:
I don't understand how to write a JavaScript to save the users progress in a game, so they can go back to the game at a later time. There is two games that really need this functionality, a php...
6
by: Josetta | last post by:
Access 2003 I've been experiencing some problems with my "monster" database the last couple of days. I imported all objects into a new database yesterday, which pretty much stopped the crashing...
3
by: jl2886 | last post by:
Hello, I have a database and when I go to close out, it says it can't save. Also, when i try to rename a form, it won't let me. I tried to export all the tables and forms in to a new database and...
8
by: bobsac | last post by:
Hey folks, i've just started with VB, I made a fairly okay loading screen for my new program i'm making but I have a problem IT WON'T LET ME SAVE AS .EXE, when I go to file it comes up with the...
1
by: Serendip | last post by:
Hi, New to 2007(SP1), but have used Access extensively since 2.0. When I invoke the query builder to make changes to a form or reports underlying query, I just can't get it to save the changes I...
4
by: markaaro | last post by:
so I've been using Microsoft Picture It! 2000 on my Vista machine since last May, then suddenly about a month ago it would no longer show the option for saving my created files in .jpg. There was a...
14
by: v4vijayakumar | last post by:
In computer based, two player, board games, how to make computer play? Are there any formal ways to _teach_ computer, to choose best possible move? I know this is kind of off-topic here. Please...
3
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello, I have the simple code below to save text, but when I click the save btn the form creates the file in the C drive, but it would not save what's in the text box. Dim sw As New...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.