[Tutor] Looking for suggestions - update

Kent Johnson kent37 at tds.net
Tue Nov 15 20:26:04 CET 2005


->Terry<- wrote:
> Today (Nov 15, 2005) at 6:08am, Kent Johnson spoke these wise words:
> - ->- I think you missed my earlier suggestion about simplifying get_button().
> 
> I tried your suggestion for changing get_button() and
> changed it to:
> 
> def get_button(click):                  # What peg was clicked?
>     for i, peg in peg_coords:
>         if click in peg:
>             return i + 1
>     return 0

Ah, my mistake, should be
    for i, peg in enumerate(peg_coords):

enumerate() is a very handy built-in, given a sequence it returns a sequence of pairs of (index, value) for each value in the list. Use it when you need the index of an item while iterating a sequence.

Kent



More information about the Tutor mailing list