[Tutor] while with function

alan.gauld@bt.com alan.gauld@bt.com
Mon Nov 18 06:36:02 2002


> I have two functions:
> 
> def add()
>     s_name=raw_input("Surname: ")
>     y=1
>     while y>0:
>        get_list()

This looks like another potential use for the 
while 1:..break idiom!

> def get_list()
>    print "1. Surname: "+s_name
>    # read option while loop here

> I had to add "global s_name, ..., ..., etc." to add() in order to get
> get_list() to display the variables.  
> Is making the add() variables global the proper solution here?

Better would be to pass the values to get_list() as a paramenter
and have the results passed back as a list. However I wonder why 
you've split the function? They both handle user input for the 
same basic structure. Why not just combine them into one slightly 
longer structure:

def add()
    s_name=raw_input("Surname: ")
    y=1
    while y>0:
        print "1. Surname: "+s_name
        # read opti

Sometimes longer function make more sense....

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld