I have a program and I want the program to be able to save question/answers so the program actually gets smarter without getting amnesia everytime its started. Can anyone help??? -
# questor.py
-
-
# define some constants for futureuse
-
-
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 newanswerdAnsNode}
-
-
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
-
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
-
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
0 1728 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Guenter Walser |
last post by:
Hello,
When using the codeline:
pickle.dump(self, file, pickle.HIGHEST_PROTOCOL)
my program runs perfectly fine:
--------------------------- Testprotocol
-----------------------------
|
by: Simon Dahlbacka |
last post by:
I'm trying to cache some information using cPickle
and I thought that using cPickle.HIGHEST_PROTOCOL would give me an
efficient representation.
However,
when trying to load the information...
|
by: sh |
last post by:
Hi guys,
Well, I have a (maybe dumb) question.
I want to write my own little blog using Python (as a fairly small but doable
project for myself to learn more deaply Python in a web context).
...
|
by: A.B., Khalid |
last post by:
I wonder if someone can explain what is wrong here. I am pickling a
list of dictionaries (see code attached) and unpickling it back using
the HIGHEST_PROTOCOL of pickle and cPickle. I am getting an...
|
by: Marcus Lowland |
last post by:
Hello, I'm fairly new to python and have read about and wanted to begin
experimenting with cpickle. As I understand, this should be a native
module in the python library. I have python 2.3 and now...
|
by: Mingus Tsai |
last post by:
Hello- please help with unpickling problem:
I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows
XPhome system.
My problem is with using cPickle to deserialize my pickled...
|
by: Jeff Poole |
last post by:
This is going to be a pretty vague message because it involves a large
block of code I'd rather avoid posting. Basically, I've been pickling
a dictionary of instances of a class I've created...
|
by: Roopesh |
last post by:
I am trying to write xml files which are inside a zip file into the
database.
In the zipfile module the function read returns bytes. What I did was
to make a blob out of the returned bytes and...
|
by: Victor Kryukov |
last post by:
Hello list,
I've found the following strange behavior of cPickle. Do you think
it's a bug, or is it by design?
Best regards,
Victor.
from pickle import dumps
from cPickle import dumps as...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |