Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 22 Online
OpenStudy (anonymous):

can anyone explain this code from the book "How to think like a computer scientist"? It's in chapter 16.3

OpenStudy (anonymous):

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

OpenStudy (anonymous):

i get everything except how the hand = hands[i%nHands] code works

OpenStudy (anonymous):

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

OpenStudy (anonymous):

so that's cycling between 1 and 0 which would deal two hands but what if there were 3 players?

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

o nvm on that whole modulus thing, that makes sense, but did you have any success at the very end on implementing the program?

OpenStudy (anonymous):

yes, i'm on assignment 12 right now actually, I've had sucess on all the assignments thus far

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!