[Tutor] Hands-on beginner's project?
Jacqui
jacqui.russell at gmail.com
Wed Jun 25 15:39:17 CEST 2008
LOL You rock! That's definitely better than my example. I can't wait to
get better at this!
:-D
On Wed, 2008-06-25 at 09:22 -0400, bob gailer wrote:
> >
>
> Even better is to define a Chapter class, with the various properties
> and methods pertinent thereto, then make each chapter an instance of
> that class.
>
> class Chapter:
>
> def __init__(self, desc, ques=None, **actions):
> self.desc = desc
> self.ques = ques
> self.actions = actions
> self.prompt = ", ".join(actions.keys())
>
> def describe(self):
> print self.desc
>
> def ask(self):
> if self.ques:
> print self.ques
> for i in range(10):
> ans = raw_input(self.prompt).lower()
> next = self.actions.get(ans, None)
> if next:
> return next
> else:
> print "Invalid response"
> else:
> print "Too many failed attempts"
>
> def main():
> chapters = [None]*11 # allow for 10 chapters starting with 1
> chapters[1] = Chapter("Ahead of you, you see a chasm.", "Attempt to
> jump over it?", y=2, n=3)
> chapters[2] = Chapter("Oops - that anvil is heavy. You die.")
> chapters[3] = Chapter("Good choice.", "Pick a direction", n=4, s=5)
> chapters[4] = Chapter("It's cold in here.", "Pick a direction", e=1, w=2)
> chapters[5] = Chapter("It's hot in here.", "Pick a direction", u=6, d=3)
> chapters[6] = Chapter("Congratulations - you found the gold.")
> next = 1
> while True:
> chapter = chapters[next]
> chapter.describe()
> next = chapter.ask()
> if not next:
> print "Game over"
> break
>
> main()
>
More information about the Tutor
mailing list