Replace for Goto

Gerrit Holl gerrit at nl.linux.org
Sun Jun 1 17:12:36 EDT 2003


Dvm5 schreef op donderdag 29 mei om 02:52:06 +0000:
> I'm used to Basic. There is a Goto/Label command, you put goto where
> you want it to come from and label where you want it to go. Simple.
> How can I do this in Python?
> 
> What I'm trying to do is create a "play again" type user interface.
> Thus, I can't alter the program to use an If command; the option has
> to be at the end, and the effect has to be at the begining! How do I
> accomplish this?
> 
> If there is another way, other than a similar thing to Goto, tell me
> that, as well as a similar thing for goto. It would probably be useful
> later on...

Dear Dvm5,

congratulations on getting into contact with Python!

Python is a very, very different language than Basic. Most - if not
all - Python programmers consider Basic a bad language and consider
"goto-style-programming" a bad style. What you need is either loops
or functions, or both.

Many Python programs look like this in pseudocode:

def foo(...):
    ...do something...

def bar(...):
    ...do something...

def main():
    ...get user input...
    ...either using raw_input() builtin function...
    ...or from a configuration file...
    ...or from the commandline...
    ...or using CGI...

if __name__ == '__main__':
    main()

What you can do, is create one or more functions for your game and
get the input in the main function. To get a loop, you can do
something like:

while raw_input("Play another game? ")[0] in "yY":
    play_game()

This way, the play_game() function is repeated as long as the user
enters yes for the question: as soon as it doesn't, the code after
this block is executed.

If this all sounds very revolutionizing for you, I'd strongly recommand
reading a book on Python for beginners. The Python website lists a lot
of books; I don't know which ones are good myself, but discussions on
this subject can be found in the archives for this newsgroup.

I hope I have been able to help your further!

yours,
Gerrit.

-- 
103. If, while on the journey, an enemy take away from him anything
that he had, the broker shall swear by God and be free of obligation. 
        -- Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list