[Tutor] Re Help with this script

John Carmona jeannot18 at hotmail.com
Thu Apr 28 03:18:47 CEST 2005


OK Alan, I thing I have seen the light!!. Here is the script that Kent and 
you asked me to look at modified:

-------------------------------------------------------------------------
def print_options():
       print "------------------------------"
       print "Options:"
       print "a. print options"
       print "f. quit the programme"
       print "------------------------------"

print_options()
choice = 0
while choice != 'f':
    print
    choice = raw_input("Choose an option: ")
    if choice == 'a':
        print "Here we go again"
        print_options()
    if choice == 'f': break

print_options()
-------------------------------------------------------------------------
Is it that if you use "while 1:" you create a recursive function? Hope I am 
right.

Thanks
JC

--------------------------------------------------------------------------------------------
PREVIOUS EMAIL

OK the situation is that I haven't still found out what the answer
is, I
>have noticed in the other hand that if I select the option "a" let's
say 4
>times, I need to enter the option "f" 4 times. I am curious to know
what the
>solution is. I have read your chapter on recursion but that did not
clear
>anything.

OK, basically the problem is that you have unintentionally created
a recursive function. Every time you call it you create a new copy
of the function. When you exit the function you wind up back in
the previous copy. So as many times as you call the function you
have to exit it the same number of times to get back to the top
of your program.

Have a think about it, and see if that makes sense.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list