1 Replies - 11169 Views - Last Post: 15 January 2013 - 11:24 AM Rate Topic: -----

#1 0bservant1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-January 13

How do I save using pickle when dealing with questions and answers?

Posted 13 January 2013 - 12:18 PM

When run, this produces an error of "NameError: global name 'Qnode' is not defined. This only happened after I inserted the pickle code to save the file. Where is my problem with my pickle code?

import cPickle as p

questorfile = 'questorbrain.data'

questorlist = []

# Write to the file
f = file(questorfile, 'w')
p.dump(questorlist, f) # dump the object to a file

del questorlist # remove the shoplist

# Read back from the storage
f = file(questorfile)
storedlist = p.load(f)
print storedlist

# 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 + "? "
            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!!!"
# 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
        topNode = Qnode('python')

        done = 0
        while not done:
# 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
            done = not yesno("Do another? ")

# immediate-mode commands, for drag-and-drop or execfile() execution
    if __name__ == '__main__':
        raw_input("press Return>")
        print "Module questor imported."
        print "To run, type: questor.run()"
        print "To reload after changes to the source, type: reload(questor)"

Is This A Good Question/Topic? 0
  • +

Replies To: How do I save using pickle when dealing with questions and answers?

#2 alexr1090   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 126
  • Joined: 08-May 11

Re: How do I save using pickle when dealing with questions and answers?

Posted 15 January 2013 - 11:24 AM

Hi. I believe your error is on 105 when you call run from inside your class. I don't think it's taking everything from that class to begin the program. It's simply calling run without knowing that Qnode exists. To get it to run I un-indented from 105 to 112. I also unindented the run function and everything inside it & changed line 87 to
result = topNode.traverse()
That made it run for me, although I'm not entirely sure it's doing what you want it to. Maybe putting all those lines at the top into run will help with that. Does that do what you're trying to accomplish?

This post has been edited by alexr1090: 15 January 2013 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1