[prog] python question. Re modulus operator and a few other things.
Guru -
haunter03 at hotmail.com
Thu Feb 13 16:14:16 EST 2003
Ok, this is something I don't understand, taken from How to think Like a
computer scientist python edition. This is only part of a larger section of
methods for the Deck class...
"
class Deck :
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
"
"The second parameter, nCards, is optional; the default is a large number,
which effectively means that all of the cards in the deck will get dealt."
Well that makes sense...
"The loop variable i goes from 0 to nCards-1. Each time through the loop, a
card is removed from the deck using the list method pop, which removes and
returns the last item in the list."
That also makes sense...
"The modulus operator (%) allows us to deal cards in a round robin (one card
at a time to each hand). When i is equal to the number of hands in the list,
the expression i % nHands wraps around to the beginning of the list (index
0). "
I don't understand how this would work. Wouldn't i % nCards find the
remainder of i (which would start at 0 correct?), but that would be 1 / 999
and the first 20 rounds would be dealing to the same person...
how would that work?
_________________________________________________________________
Hotmail now available on Australian mobile phones. Go to
http://ninemsn.com.au/mobilecentral/hotmail_mobile.asp
More information about the Programming
mailing list