[Tutor] while with function

alan.gauld@bt.com alan.gauld@bt.com
Tue Nov 19 04:11:43 2002


> So maybe get_list() should be renamed edit_list(); but in any 
> event, it refers to the list of variables for each address book entry 

OK So in that case pass the entry in as a parameter. You can have 
a default parameter of None and if None is passed it effectively 
creates a new entry. The modified(or created) entry is then returned

This avoids the nightmare of accessing the same set of global 
variables from lots of different functions.

> I prefer not to mess with classes at this point in my python 
> education.  

Thats OK, It just happens that this is one application where they 
are perfectly suited, but you an do it without...

> following it in some cases.  And none of it appears to allow 
> me to review and correct the new address book entry before 
> I "commit" it to the database.

Well we didn't know until now that that was a requirement.
Personally I'd use two functions, one to dsplay and entry and 
a separate one to edit an entry. You then have a loop something
like:

def is_ok(entry):
   # display entry content
   # ask if user is happy
   # return y/n

def edit_entry(entry=None):
   if entry == None:
      # create new entry with default values
   #foreach field in entry
   #get input per field
   return entry

while not is_ok(entry):
    entry = edit_entry(entry)
save_entry(entry)
    


Alan G.