can anyone explain this code from the book "How to think like a computer scientist"? It's in chapter 16.3
def deal(self, hands, nCards=999): nHands = len(hands) for i in range(nCards): if self.isEmpty(): break # break if out of cards card = self.popCard() # take the top card hand = hands[i % nHands] # whose turn is next? hand.addCard(card) # add the card to the hand
i get everything except how the hand = hands[i%nHands] code works
hands being a list of players, the remainder of current index/number of hands is the index used to pull a hand from the list of players to play two hands: 1%2 = 1 2%2 = 0 3%2 = 1 it just cycles through remainders until it empties the deck hope that was remotely clear
so that's cycling between 1 and 0 which would deal two hands but what if there were 3 players?
and i just finished going through the example on chapter 16 and now im getting an error halfway through the game... Traceback (most recent call last): File "<pyshell#58>", line 1, in <module> game.play(["Allen", "Joe", "Kobe"]) File "C:/Python27\cards.py", line 138, in play matches = matches + self.playOneTurn(turn) TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' anyone else get this?
o nvm on that whole modulus thing, that makes sense, but did you have any success at the very end on implementing the program?
yes, i'm on assignment 12 right now actually, I've had sucess on all the assignments thus far
Join our real-time social learning platform and learn together with your friends!