[Tutor] while with function
fleet@teachout.org
fleet@teachout.org
Mon Nov 18 10:14:02 2002
I split the functions because it seemed to me I *had* two functions.
add() allows input for an address book; get_list() allows me to repeatedly
(if necessary) display the list of variables and make corrections before
adding the entry to a file. (I also had it in mind to use get_list() in
an edit() function - haven't gotten there yet.)
Regards,
- fleet -
On Mon, 18 Nov 2002 alan.gauld@bt.com wrote:
> > 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
>