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!!! - # questor.py
-
-
# define some constants for future use
-
-
kQuestion = 'question'
-
kGuess = 'guess'
-
-
# define a function for asking yes/no questions
-
def 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 method
-
def __init__(self,guess):
-
self.nodetype = kGuess
-
self.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 response
-
def 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 item
-
def makeQuest( self, question, newitem, newanswer ):
-
-
# create new nodes for the new answer and old answer
-
newAnsNode = Qnode(newitem)
-
oldAnsNode = Qnode(self.desc)
-
-
# turn this node into a question node
-
self.nodetype = kQuestion
-
self.desc = question
-
-
# assign the yes and no nodes appropriately
-
self.nodes = {newanswer:newAnsNode, not newanswer:oldAnsNode}
-
-
-
def traverse(fromNode):
-
# ask the question
-
yes = 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 node
-
return fromNode
-
-
# if it's a question node, then ask another question
-
return traverse( fromNode.nextnode(yes) )
-
-
def run():
-
# start with a single guess node
-
try:
-
f = file('questor.dat', 'r')
-
topNode = pickle.load(f)
-
f.close()
-
except:
-
topNode = Qnode('python')
-
-
done = 0
-
while not done:
-
try:
-
f = file('questor.dat', 'w')
-
pickle.dump(topNode, f)
-
f.close()
-
except:
-
print "Could not save!"
-
# ask questions till we get to the end
-
result = traverse( topNode )
-
-
# if result is a node, we need to add a question
-
if (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 done
-
print
-
done = not yesno("Do another? ")
-
#print
-
-
# immediate-mode commands, for drag-and-drop or execfile() execution
-
if __name__ == '__main__':
-
run()
-
print
-
raw_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
2 2619
You can only pickle a class instance. Start herefor a class tutorial.
@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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |