This might be from a 3rd hour in our "casino math" segment. There's no "deep math" here, just getting familiar with dot notation, methods disguised as attributes (properties):
import random class Deck: def __init__(self): self.deck = [ (suit + str(rank)) for suit in ['H','D','C','S'] for rank in range(1,14) ] + ['joker']*2 def deal_off_top(self): if len(self.deck) > 0: return self.deck.pop(0) def deal_off_bottom(self): if len(self.deck) > 0: return self.deck.pop(-1) def shuffle(self): random.shuffle(self.deck) @property def howmany(self): return len(self.deck) Interactively... (inserting some blank lines for readability): >>> from casino_math import * >>> thedeck = Deck() >>> thedeck.shuffle() >>> thedeck.deck ['S10', 'joker', 'S1', 'C11', 'C1', 'C8', 'C3', 'D1', 'S6', 'S3', 'D12', 'D5', 'D10', 'S12', 'C2', 'D6', 'S2', 'D2', 'C9', 'H3', 'C13', 'D8', 'C7', 'D4', 'H6', 'H11', 'H2', 'D13', 'S11', 'H1', 'H8', 'H7', 'S8', 'H12', 'S4', 'D9', 'S9', 'C12', 'H9', 'D7', 'S5', 'S7', 'C5', 'C10', 'H5', 'D11', 'C4', 'joker', 'C6', 'S13', 'H4', 'H13', 'H10', 'D3'] >>> thedeck.deal_off_top() 'S10' >>> thedeck.deal_off_top() 'joker' >>> thedeck.deal_off_bottom() 'D3' >>> thedeck.deal_off_bottom() 'H10' >>> thedeck.howmany 50 >>> http://www.flickr.com/photos/17157...@n00/3876500673/sizes/o/ (akbar font) Of course this is a rather primitive deck without explicit aces or royalty, just 1-13 in each suit plus two jokers. The next step would be for students to write a Hand class i.e. that which receives from the deck. Before ya know it, we're playin' poker or twenty-one or whatever well-known game of skill 'n chance. On break, we encourage playing with Pysol maybe... http://www.pysol.org/ http://pysolfc.sourceforge.net/ http://tktable.sourceforge.net/tile/ http://pygames.sourceforge.net/ Kirby 4D _______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig