3
class HandOfCards:

    def __init__ (self, numCardsInHand):
        self.list = []

        all_rank = random.randint(1, 13)
        random_suit = ['d', 'c', 'h', 's']
        all_suit = random.choice(random_suit)


        for count in range(numCardsInHand):
            self.list.append(Card(all_rank, all_suit))


    def __str__(self):
        all_items = '\n'.join(map(str, self.list))
        return all_items

I'd like to create a number of random objects, but with the loop

for count in range(numCardsInHand):
            self.list.append(Card(all_rank, all_suit))

it turned out I actually created numCardsInHand of exactly same objects. It seems like that the random module doesn't work on my for loop. I have problem figuring it out.

3 Answers 3

2

You need to make the calls to the random module within your loop:

    for count in range(numCardsInHand):
        all_rank = random.randint(1, 13)
        random_suit = ['d', 'c', 'h', 's']
        all_suit = random.choice(random_suit)
        self.list.append(Card(all_rank, all_suit))
Sign up to request clarification or add additional context in comments.

Comments

2

The random module only assigns the value to the variable and the variables is fixed to it. It does not reassign everytime you call the variable (that would be weird). In other words, you should call the function again everytime you want a new value. In your case,

for count in range(numCardsInHand):
    self.list.append(Card(random.randint(1, 13), random.choice(random_suit))

Another way you can do this is to assign a variable to the function itself. So your code would look like:

all_rank = random.randint
random_suit = ['d', 'c', 'h', 's']
all_suit = random.choice

for count in range(numCardsInHand):
    self.list.append(Card(all_rank(1, 13), all_suit(random_suit))

Though that may be confusing.

One last note, try not to use list to store lists. list is a built-in in Python and may conflict the next time you try to convert anything to a list.

Comments

2

all_rank is not a function that generates a random rank; it is a single, randomly chosen rank, as the result of a single call to random.choice. (Likewise for all_suit.)

However, even if you called random.choice and random.randint each time you call Card, you have a bigger problem: you can't guarantee that you won't pick the same card twice. The solution is simple: create a deck of cards, then use random.sample to pick from the deck.

# All 52 possible cards
deck = [Card(rank, suit) for rank in range(1,14) for suit in ['d', 'h', 'c', 's']]
hand = random.sample(deck, numCardsInHand)

To choose multiple hands, you pick one hand, remove the cards chosen from the deck, then sample again. However, it's simpler to just shuffle the deck and deal them.

deck = [Card(rank, suit) for rank in range(1,14) for suit in ['d', 'h', 'c', 's']]
random.shuffle(deck)
hand1 = deck[0:numCardsInHand]
hand2 = deck[numCardsInHand:2*numCardsInHand]
# etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.