On Mon, 2009-07-20 at 12:26 -0700, Phillip B Oldham wrote:
> On Jul 20, 6:08 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
> > The main reason why you need both lists and tuples is that because a tuple
> > of immutable objects is itself immutable you can use it as a dictionary
> > key.
> 
> Really? That sounds interesting, although I can't think of any real-
> world cases where you'd use something like that.

Well, if you wanted to index a dictionary by coordinates, you might do
something like this:


fleet = {}
fleet[9,4] = 'destroyer'
fleet[8,4] = 'destroyer'
fleet[3,5] = 'aircraftcarrier'
fleet[4,5] = 'aircraftcarrier'
fleet[5,5] = 'aircraftcarrier'
fleet[6,5] = 'aircraftcarrier'
fleet[8,0] = 'battleship'
fleet[8,1] = 'battleship'
fleet[8,2] = 'battleship'


def checkattack(x, y, fleet):
    if x,y in fleet:
        return "You hit my %s' % fleet[x,y]

Maybe not the best implementation of Battleship, but you get the idea.



-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to