[Tutor] Hands-on beginner's project?

Jacqui jacqui.russell at gmail.com
Wed Jun 25 12:16:24 CEST 2008


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?"
	
	
so that in your code, instead of 

> 0100  print "Ahead of you, you see a chasm.
> 0200 jump = raw_input("Do you wish to try jumping over it? Y/N")
> 0300 if jump = Y:
> 0400       goto 1700
> 0500 if jump = N:
> 0600      goto 2100

you could have

> 0100  print "Ahead of you, you see a chasm."
> 0200 jumpQ = raw_input("Do you wish to try jumping over it? y/n: ")
> 0300 if jumpQ == "y":
> 0400      chapter2()
> 0500 elif jumpQ == "n":
> 0600      chapter3()

I just tried this bit out

def chapter1():
    print "this is an interactive story test"
    print

def chapter2():
    print "You have chosen to leap across the chasm"
    print "Sadly you forgot you are carrying an anvil"
    print "What part of b-bye don't you understand?"

def chapter3():
    print "You wisely chose to turn back home"
    print "The anvil you are carrying would surely cause you"
    print "to plummet to your death"



def main():
    chapter1()
    print "You come across a chasm, would you like to jump?"
    jumpQ = raw_input("y/n: ")
    if jumpQ == "y":
        chapter2()
    elif jumpQ == "n":
        chapter3()
main()

and it worked although I have doubts about how good it would be overall
once the story got very involved. I'm interested to see what others
think.

Jacqui



More information about the Tutor mailing list