How to create a deck of cards

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Miguel Valenzue
    New Member
    • Dec 2010
    • 39

    How to create a deck of cards

    Hi there. Thanks for your help.
    I am experimenting with lists and dictionaries and would like to make a dictionary of playing cards as my eventual outcome.
    I know I can write it out by hand but this is an excercise so I can learn about data etc. (this is not homework, I'm just learning python.

    What I have accomplished so far.
    I have managed to make a nested list of the name of the card, and its associated value. i.e. [['2 of hearts', 2] etc..

    Here is my code.
    I would like to know if there are any suggestions to improve upon it.
    Thanks
    Miguel
    PS I know that the Ace has more than one value but I will work on that soon enough.

    Code:
    ##Declare a basic list of items that all cards will have
    cards = [['2',2], ['3',3], ['4',4],
                ['5',5], ['6',6], ['7',7], ['8',8], ['9',9],
                ['10',10], ['Jack',10], ['Queen',10], ['King',10], ['Ace',11]]
    
    ##declare a separate list for each suit
    hearts = []
    clubs = []
    spades =[]
    diamonds =[]
    
    ##Now create the list of cards for each suit
    for x,y in cards:
        hearts_row=[]
        clubs_row=[]
        spades_row=[]
        diamonds_row=[]
        for item in cards[0][0]:
            hearts_row.append(x + ' of hearts')
            hearts_row.append(y)
            
            clubs_row.append(x + ' of clubs')
            clubs_row.append(y)
    
            spades_row.append(x + ' of spades')
            spades_row.append(y)
    
            diamonds_row.append(x + ' of diamonds')
            diamonds_row.append(y)
    
        hearts.append(hearts_row)
        clubs.append(hearts_row)
        spades.append(hearts_row)
        diamonds.append(hearts_row)
    
    
    ##combine all suits into one deck
        
    
    deck = hearts + clubs + spades + diamonds
    ##Print Deck
    print (deck)
    Here is my output!

    [['2 of hearts', 2], ['3 of hearts', 3], ['4 of hearts', 4], ['5 of hearts', 5], ['6 of hearts', 6], ['7 of hearts', 7], ['8 of hearts', 8], ['9 of hearts', 9], ['10 of hearts', 10], ['Jack of hearts', 10], ['Queen of hearts', 10], ['King of hearts', 10], ['Ace of hearts', 11], ['2 of hearts', 2], ['3 of hearts', 3], ['4 of hearts', 4], ['5 of hearts', 5], ['6 of hearts', 6], ['7 of hearts', 7], ['8 of hearts', 8], ['9 of hearts', 9], ['10 of hearts', 10], ['Jack of hearts', 10], ['Queen of hearts', 10], ['King of hearts', 10], ['Ace of hearts', 11], ['2 of hearts', 2], ['3 of hearts', 3], ['4 of hearts', 4], ['5 of hearts', 5], ['6 of hearts', 6], ['7 of hearts', 7], ['8 of hearts', 8], ['9 of hearts', 9], ['10 of hearts', 10], ['Jack of hearts', 10], ['Queen of hearts', 10], ['King of hearts', 10], ['Ace of hearts', 11], ['2 of hearts', 2], ['3 of hearts', 3], ['4 of hearts', 4], ['5 of hearts', 5], ['6 of hearts', 6], ['7 of hearts', 7], ['8 of hearts', 8], ['9 of hearts', 9], ['10 of hearts', 10], ['Jack of hearts', 10], ['Queen of hearts', 10], ['King of hearts', 10], ['Ace of hearts', 11]]
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Here you go buddy, this is the easier way of doing it.

    I'll walk you through the thought process:

    1. First outline what you have: Card values, card suites:

    Code:
    card_values = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
    card_suites = ['Hearts', 'Clubs', 'Diomands', 'Spades']
    2. Then you need to combine these lists (multiply them), but still leave them in a list. You know you need to loop both lists, which means you have two for-loops. In python, list comprehension is a great way to do it. It's hard to understand at first (it was for me) but eventually you'll get it the more you practice it.

    Code:
    result = [[v + ' of ' + s,v] for s in card_suites for v in card_values]
    Your output in result looks like this:

    Code:
    [[v + ' of ' + s,v] for s in card_suites for v in card_values]
    [['2 of Hearts', '2'], ['3 of Hearts', '3'], ['4 of Hearts', '4'], ['5 of Hearts', '5'], ['6 of Hearts', '6'], ['7 of Hearts', '7'], ['8 of Hearts', '8'], ['9 of Hearts', '9'], ['10 of Hearts', '10'], ['Jack of Hearts', 'Jack'], ['Queen of Hearts', 'Queen'], ['King of Hearts', 'King'], ['Ace of Hearts', 'Ace'], ['2 of Clubs', '2'], ['3 of Clubs', '3'], ['4 of Clubs', '4'], ['5 of Clubs', '5'], ['6 of Clubs', '6'], ['7 of Clubs', '7'], ['8 of Clubs', '8'], ['9 of Clubs', '9'], ['10 of Clubs', '10'], ['Jack of Clubs', 'Jack'], ['Queen of Clubs', 'Queen'], ['King of Clubs', 'King'], ['Ace of Clubs', 'Ace'], ['2 of Diomands', '2'], ['3 of Diomands', '3'], ['4 of Diomands', '4'], ['5 of Diomands', '5'], ['6 of Diomands', '6'], ['7 of Diomands', '7'], ['8 of Diomands', '8'], ['9 of Diomands', '9'], ['10 of Diomands', '10'], ['Jack of Diomands', 'Jack'], ['Queen of Diomands', 'Queen'], ['King of Diomands', 'King'], ['Ace of Diomands', 'Ace'], ['2 of Spades', '2'], ['3 of Spades', '3'], ['4 of Spades', '4'], ['5 of Spades', '5'], ['6 of Spades', '6'], ['7 of Spades', '7'], ['8 of Spades', '8'], ['9 of Spades', '9'], ['10 of Spades', '10'], ['Jack of Spades', 'Jack'], ['Queen of Spades', 'Queen'], ['King of Spades', 'King'], ['Ace of Spades', 'Ace']]
    The whole thing in only three lines of code!
    Code:
    values = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
    suites = ['Hearts', 'Clubs', 'Diomands', 'Spades']
    deck = [[v + ' of ' + s,v] for s in suites for v in values]
    You might say you're missing the values for Jack Queen and King! Ah! I'll leave this as an exercise to you, but if you can't figure it out, I'll help you here or post another question. HINT: Turn the value list into a dict.
    Last edited by dlite922; Sep 12 '14, 04:13 PM. Reason: Clarify

    Comment

    • Miguel Valenzue
      New Member
      • Dec 2010
      • 39

      #3
      Thanks so much dlite922. I feel like an amateur asking these questions and it's nice to see that python can do this with such little code.
      Miguel

      Comment

      Working...