Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 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!
Latest Questions
Breathless: Spooky witch but cute
3 hours ago 3 Replies 0 Medals
Arriyanalol: help
3 hours ago 10 Replies 2 Medals
Arriyanalol: @tinydinoUwU stop trying to find a argument u blad lil boy
1 day ago 5 Replies 3 Medals
Jaded012023: Please tell me what you all think of this song
6 hours ago 6 Replies 1 Medal
Arriyanalol: bro how
6 hours ago 2 Replies 3 Medals
Arriyanalol: cant wait for the new bluey movie in 2027
1 day ago 12 Replies 2 Medals
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!