0

I'm currently doing a python project that requires me to make a game; I have defined a class called Game that when initialized takes P1and P2 (Player class instances).

I then defined a list called deck, which then I shuffled and redefined as the new shuffled deck. The issue is when I print deck it prints None.

import random
class Game():
    deck = []
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2
        self.__generateCards()
    def __generateCards(self):
        colors = ['red', 'black', 'yellow']
        for x in range(0,3):
            for y in range(1,11):
                li = [colors[x], y]
                self.deck.append(li)
        self.deck = random.shuffle(self.deck)
        print(self.deck)
dspencer
  • 4,297
  • 4
  • 22
  • 43
Joiie
  • 11
  • 1
  • Unrelated, but you DONT want to make `deck` a class attribute (class attributes are shared between all instances of the class). Remove `deck = []` from the class block and add `self.deck = []` in the `__init__` _before_ the `self._generatedCards()` call. – bruno desthuilliers Apr 06 '20 at 08:25
  • Also, don't use double leading underscores for implementation attributes or methods - the convention is one single leading underscore. The double-underscore thing (which triggers a name mangling mechanism) is only to prevent from accidental name clashes, and is really of very little use in practice, even in huge frameworks. – bruno desthuilliers Apr 06 '20 at 08:28

5 Answers5

0

random.shuffle returns None, it shuffles in place and therefore self.deck is None.

Tom Ron
  • 5,906
  • 3
  • 22
  • 38
0

random.shuffle() doesn't return anything.

Use instead random.sample() like in docs recommended.

...
   self.deck = random.sample(self.deck, k=len(self.deck))
...
zypro
  • 1,158
  • 3
  • 12
  • 33
0

Take a look at this resource: https://www.w3schools.com/python/ref_random_shuffle.asp

Python's random.shuffle(list) shuffles in place, so there is no need to set your variable to the outcome.

NolanRudolph
  • 106
  • 11
0

''' import random

deck = list(itertools.product(range(1, 14), ['red', 'black', 'yellow']))

random.shuffle(deck)

print("Player one got")

for p1 in range(5): print(deck[p1][0], "of", deck[p1][1])

print("\n player tow got") for p2 in range(5): print(deck[p2][0], "of", deck[p2][1])

And issue is after append line. '''

Shah Vipul
  • 625
  • 7
  • 11
0

It has been already said in other comments, random.shuffle(list) shuffles in place. So what you can do is:

import random
class Game():
    deck = []
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2
        self.__generateCards()
    def __generateCards(self):
        colors = ['red', 'black', 'yellow']
        for x in range(0,3):
            for y in range(1,11):
                li = [colors[x], y]
                self.deck.append(li)
        random.shuffle(self.deck)    #here, don't do self.deck = random.shuffle(self.deck)
        print(self.deck)
advay rajhansa
  • 1,129
  • 9
  • 17