At Wednesday 29/11/2006 06:48, B Shyam Sundar wrote:

I am a newbie as far as python is concerned ... I am trying to write a code for playing bridge in obj oriented manner ..

Well ... i have this small problem:

class hand:
    def __init__(self,set_of_cards=[]):
        self.set_of_cards=set_of_cards
    card_played_flag =0
    def play(played_card):
        for i in self.set_of_cards:
            if self.set_of_cards[i] == played_card:
                self.set_of_cards.remove (played_card)
     def sort_hand():
        for i in self.set_of_cards:
            for j in self.set_of_cards:
                if self.set_of_cards[j].face<self.set_of_cards[i].face
self.set_of_cards [j],self.set_of_cards[i]=self.set_of_cards[i],self.set_of_cards[j]
         for i in self.set_of_cards:
            for j in self.set_of_cards:
                if self.set_of_cards[j].suit<self.set_of_cards[i].suit

self.set_of_cards[j],self.set_of_cards[i]=self.set_of_cards[i],self.set_of_cards[j]

I have created card as a class with the attributes of suit face so .. card(suit,face)

So my question is this ---

The compiler doesnt recognise that there is self.set_of_cards[j] can be a card. It gives a syntax error.
Also I have not created any instance of the class (Can that be a problem)

(The compiler knows very few about your cards... Python is a very dynamic language, a lot of things happens at runtime)

Without compiling your example, I can see these syntax errors:
1) In Python indentation matters. sort_hand must have the same indentation as play (and the other methods).
2) Syntax for the if statement - you need a final ":"
if condition: something
if condition:
    do something
    and do other thing
    and do another thing

So how do i fix this?
Also can I specify that the list set_of_cards has only card objects? IF yes how?

Short answer: don't bother. (Yes, you *could* inherit from list and restrict the contained items, but it's seldom used).

You don't have to sort manually, this should work: set_of_cards.sort(key=lambda card: (card.suit,card.face))

And don't use a mutable initializer in __init__: either use an empty tuple () or use None and check for it in the method body:
def __init__(self, set_of_cards=None):
    if set_of_cards is None: set_of_cards=[]
    self.set_of_cards = set_of_cards


--
Gabriel Genellina
Softlab SRL
__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to