'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.
Does anyone know how to fix this?
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BrokenBenchmark |
