answers.py v0.0.1 - source
Pyenos
llee4131 at users.sourceforge.net
Thu Dec 28 15:41:16 EST 2006
#!/usr/bin/python
################################################################
# answers.py --- A simple answer-bot.
# Copyright 2006 Logan Lee
#
# RELEASE NOTES:
#
# - I have fixed an error where the program crashes on query of
#+unknown string to answers dictionary.
# - Rephrased 'Do you have a problem?' message in menu to
#+"What is your problem?"
#
# MESSAGE:
#
# Now the program is perfectly usable to an extent that was
#+intended! Previous advice on the code v0.01 is by in large
#+not implemented in this version, but I will learn from it.
#+Thanks.
#
################################################################
################################################################
# IMPORTANT: DON'T FORGET TO SET ANSWER_PATH TO YOUR PREFERENCE!
global ANSWER_PATH
ANSWER_PATH=""
################################################################
VERSION="0.01001"
class Problem:
def __init__(self):self.problem=""
def set(self,problem):self.problem=problem
def get(self):return self.problem
class Solution:
def __init__(self):self.solution=""
def set(self,solution):self.solution=solution
def get(self):return self.solution
class Storage:
def add(self,problem,solution):
self.answers[problem]=solution
def save(self):
import pickle
try: pickle.dump(self.answers,file(ANSWER_PATH+".answers","w"));print "Answers saved"
except: print "Couldn't save answers. Something went wrong"
def load(self):
import pickle
try: self.answers=pickle.load(file(ANSWER_PATH+".answers"));print "Answers loaded"
except: print "Couldn't load answers. Something went wrong or this may be your first time running"
def getAnswer(self,problem):
answer=""
try: answer=self.answers[problem]
except: print "I don't have the answer to that problem"
return answer
def __init__(self,answers={}):
self.answers=answers
class Console:
class Menu:
global storage
storage=Storage()
class Level0:
def do(self):
import sys
action=sys.exit()
class Level1:
def do(self):
problem=Problem();solution=Solution()
text=("What is your problem? $ ","Do you have the solution? $ ","no idea","Try again with your common sense")
commands=("answer","load(disabled, already loaded)","quit","help")
problem.set(raw_input(text[0]))
if problem.get()==commands[0]:return 4
#elif problem.get()==commands[1]:storage.load();return 1
elif problem.get()==commands[2]:return 0
elif problem.get()==commands[3]:print commands;return 1
solution.set(raw_input(text[1]))
if solution.get()==text[2]:print text[3];solution.set(raw_input(text[1]))
if raw_input("Your solution to '%s' is '%s', correct? $ " % (problem.get(),solution.get()))=="yes":storage.add(problem.get(),solution.get());print "Answer confirmed";return 2
else: return 1
class Level2:
def do(self):
if raw_input("Should I save the answer? $ ")=="yes":
storage.save()
return 3
else: return 1
class Level3:
def do(self):
text=("Do you have another problem?"+" $ ","Do you want to quit?"+" $ ")
if raw_input(text[0])=="yes":
return 1
elif raw_input(text[1])=="yes":return 0
else: return 3
class Level4:
def do(self):
text=("What answer do you seek? $ ","Do you want to ask again? $ ")
problem=raw_input(text[0]);answer=storage.getAnswer(problem)
print "Answer to '%s' is '%s'" % (problem,answer)
if raw_input(text[1])=="yes":return 4
else: return 1
def start(self):
storage.load()
level=1
while 1:
if level==0:
Console.Menu.Level0().do()
if level==1:
level=Console.Menu.Level1().do()
if level==2:
level=Console.Menu.Level2().do()
if level==3:
level=Console.Menu.Level3().do()
if level==4:
level=Console.Menu.Level4().do()
def start(self):Console.Menu().start()
if __name__=="__main__":
Console().start()
--
Logan Lee aka Pyenos
More information about the Python-list
mailing list