Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error while trying to shuffle a deck of cards

I am trying to shuffle a deck of cards and I keep getting this error which I don’t know how to solve. I am new to python and OOP so I think this should just be something that I overlooked.

This is my code:

class Deck:
    def __init__(self):
        self.deck = []

        for suit in suits:
            for rank in ranks:
                card_created = Card(suit, rank)
                self.deck.append(card_created)

    def __str__(self):
        deck_comp = ""
        for card in self.deck:
            deck_comp += "\n"+ card.__str__()
        return "The deck has: " + deck_comp

    def shuffle(self):
        random.shuffle(self.deck)

    def deal_one(self):
        return self.deck.pop()

new_deck = Deck()

print(new_deck.shuffle())

When I run the program I only get "None". However, before shuffling the deck, I get all the existing cards in order.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does anyone know how to fix this?

>Solution :

Your shuffle() function doesn’t return any value (and neither does random.shuffle(), which shuffles the list in-place).

Instead, you should perform the call to .shuffle() and print() on separate lines:

new_deck.shuffle()
print(new_deck.deck)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading