Hello everyone !

I am a still a beginner in Python (and have no knowledge in other languages), and today I got a reaction from my script that I find unexpected.

Here's the code:
I want to use the buildOneCard function to create a card (a couple of values), by using datas stock in a dictionnary in another function (here, frenchCard).

class Card:
    """
    Define the different kinds of play card.
    
        A card is constituted of several elements (usually two) that reflect its
        value.
        These elements are tuples 
        
   
    """
    
    def __init__(self):
        "Nothing here yet"

    def frenchCard(self):
        """
        Define a card like the ones used in poker. Return a dictionnary.
    
            elements = the dictionnary used to handle data
            rank = The rank of the card (main value discriminant)
            color = The color order is taken in account when the ranks are equal
    
        """
        return {'rank' : (2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace'),
        'color' : ('Spade', 'Club', 'Diamond', 'Heart')}

        
    def buildOneCard(self, card_type=''):
        "Will hopefully build a card when given a type of card"
        self.card_type = card_type
        for elements in getattr(self, '%s' % card_type)():
            print elements

When I launch it in the shell, it returns rank and color... and nothing else !

If I replace print with return, it says that elements is not defined !

And I am pretty clueless, so if someone can explain it to me, thanks in advance !

if D is a dictionary a code like

for x in D:
    print x

will print only the keys of the dictionary. If you wnat the keys and values, you should write

for k, v in D.iteritems():
    print k, v

Why use the getattr? I think gribs way of doing it probably is what you want.

Give it a little test to familiarize yourself with dictionaries:

cards = {'rank' : (2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace'),
        'color' : ('Spade', 'Club', 'Diamond', 'Heart')}

for card in cards:
    print card
    print cards[card]

"""
my output --->
color
('Spade', 'Club', 'Diamond', 'Heart')
rank
(2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace')
"""

Thank you all ! You are quick !

I understand better this stuff now, hope that my brain will assimilate it as soon as possible !

Keepin' on learnin' ...

Me again !

Looking for the light I began looking through the 'Starting Python' thread and look what I have found :

def create_cards():
    """
    create a list of 52 cards
    suit: club=C, diamond=D, heart=H spade=S
    rank: ace=A, 10=T, jack=J, queen=Q, king=K, numbers=2..9
    ace of spade would be SA, 8 of heart would be H8 and so on ...
    """
    return [ suit + rank for suit in "CDHS" for rank in "A23456789TJQK" ]

It is at the same time simple and yet out of my reach ... Raging ...

Still, thanks for the former answers !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.