bob gailer wrote:
Jacqui wrote:
Hi, I'm a total newbie too, and I'm kind of replying to see if my
instinct on the whole GOTO thing is correct. It's hard to learn a
language without any feedback!

I used GW and Color Basic when I was a kid so I know all about GOTO (and
it was a mess! I programmed one of those interactive stories in grade 12
using it, it took all semester and was anything but elegant!)

I would expect with Python, instead of using GOTO you use defined
functions.

So for instance, you could define chapters as functions


def chapter2():
    print "You've chosen to leap over the chasm"
    print "too bad you forgot you were carrying an anvil"
    print "What part of b-bye don't you understand?"

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"
# CORRECTION - indent
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()


--
Bob Gailer
919-636-4239 Chapel Hill, NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to