mercredi 28 octobre 2015

Function returning too many poker cards and duplicates

I'm trying to get the second function to call the first function, check for duplicates, then return 2 cards (made up of 2 items from RANK and 2 from SUIT). It's returning more than 2 cards though and I don't know why.

It's returning two lists in the output. 2 of which are duplicates, but I don't know if it's how I appended them in the first function that's at fault or something else.

#!/usr/bin/python3
from random import choice
from random import randint

class Cards(object):
    RANK = [1,2,3,4,5,6,7,8,9,10,'J','Q','K','A']
    SUIT = ['Club','Diamond','Heart','Spade']
 #Creates One random card/suit combo   
    def picker(self):
        pick=[]
        pick.append(choice(self.RANK))
        pick.append(choice(self.SUIT))
        return pick

#'Should' create 2 cards, check that they aren't dupes, and return them.
    def hole(self):
        hold=[]
        nodup=[]
        while len(hold)<5:
            nodup.append(self.picker())
            if nodup not in hold:
                hold.append(nodup)
            else:
                hold.append(self.picker())
                continue
        return hold

When I call the function, I get this-

>>> from cards import Cards
>>> test=Cards().hole()
>>> test
    [[[2, 'Heart'], ['Q', 'Spade'], [2, 'Diamond'], [9, 'Club'], [1, 'Diamond']], [5, 'Heart'], [5, 'Heart'], [5, 'Club'], ['K', 'Heart']]

I want-
>>> [2, 'Heart'], ['Q', 'Spade']




Aucun commentaire:

Enregistrer un commentaire