[Edu-sig] Cards n stuff...
Jeffrey Elkner
jeff@elkner.net
14 Mar 2001 13:34:24 -0500
Thanks Kirby!
This was really helpful. The one line I would change is:
ranks = ['Ace']+map(str,range(2,11))+['Jack','Queen','King']
I haven't discussed map at all yet, and actually don't plan to.
In using Python to teach, simplicity takes on even more importance
than it usually has (and it is always important ;-)
Python is such a wonderful language for teaching precisely
because it "fits your brain". In the case of students new
to computing, "fitting their brains" means minimizing the number of
previous concepts needed to understand a new concept.
In truth, what is good for new learners is good in general,
only more so.
Thanks again for your help!
jeff
On 13 Mar 2001 11:04:51 -0800, Kirby Urner wrote:
>
> Hi Jeff --
>
> Here's a slightly modified version of your code, including
> some ideas about shuffling:
>
> import random
>
> class Card:
>
> def __init__(self, suite, rank):
> self.suite = suite
> self.rank = rank
>
> def __repr__(self):
> return self.rank + ' of ' + self.suite
>
> class Deck:
>
> suites = ['Hearts','Diamonds','Spades','Clubs']
> ranks = ['Ace']+map(str,range(2,11))+['Jack','Queen','King']
>
> def __init__(self):
> self.cards = []
> for suite in Deck.suites:
> for rank in Deck.ranks:
> newCard = Card(suite, rank)
> self.cards.append(newCard)
>
> def shuffle(self):
> newdeck = []
> while len(newdeck)<52:
> randomcard = random.choice(self.cards)
> newdeck.append(randomcard)
> self.cards.remove(randomcard)
> self.cards = newdeck
>
>
>
> Note: I manually truncated/edited the list of cards to save
> space.
>
> >>> mydeck = cards.Deck()
> >>> mydeck.cards
> [Ace of Hearts, 2 of Hearts, 3 of Hearts, 4 of Hearts, 5 of Hearts,
> 6 of Hearts, 7 of Hearts, 8 of Hearts, 9 of Hearts, 10 of Hearts,
> Jack of Hearts, Queen of Hearts, King of Hearts, Ace of Diamonds...]
>
> >>> mydeck.shuffle()
> >>> mydeck.cards
> [Ace of Spades, 9 of Hearts, 7 of Spades, Queen of Diamonds, 3 of Hearts,
> Jack of Spades, 10 of Clubs, 4 of Spades, 10 of Hearts, Queen of Spades,
> 7 of Diamonds, Ace of Hearts, Queen of Hearts, Queen of Clubs, 6 of Spades,
> King of Hearts...]
> >>> len(mydeck.cards)
> 52