dimanche 9 mai 2021

random.shuffle doesn't shuffle

I'm building a deck of cards that has shuffling function. I'm using random.shuffle, but it is not shuffling at all. When I run the following codes, the whole deck is printed out in good order. Please have a look, thanks!

import random


class Card(object):
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value

    def show(self):
        print("{} of {}".format(self.value, self.suit))
        return self.value


class Deck(object):
    def __init__(self):
        self.cards = []
        self.build()

    def build(self):
        for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
            for v in range(1, 14):
                self.cards.append(Card(s, v))
                print("{} of {}". format(v, s))

    def show(self):
        for cards in self.cards:
            print(cards.show())

    def shuffle(self):
        random.shuffle(self.cards)
        return self.cards

    def draw_card(self):
        return self.cards.pop()


deck = Deck()
deck.shuffle()



Aucun commentaire:

Enregistrer un commentaire