Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I think the forum is a great idea, especially for those of us in consulting engineering. Keep up the good work!..."

Geography

Where in the world do Tek-Tips members come from?
teknoob (IS/IT--Management)
28 Aug 10 4:53
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
mikrom (Programmer)
28 Aug 10 10:17
You get the exceprion "Could not save!"
in the function run() when you try to do

CODE

pickle.dump(topNode, f)
here:

CODE

         try:
            f = file('questor.dat', 'w')
            pickle.dump(topNode, f)
            f.close()
         except:
            print "Could not save!"
pickle is a python module and you forgot to import it before usage.

Import the pickle module on beginning of your code for example here

CODE

# questor.py
import pickle
# define some constants for future use
...
and it will be ok.
teknoob (IS/IT--Management)
29 Aug 10 11:02
Wow, I'm dumb,
I completely overlooked that
Thank you So much
Have a great Day!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close