










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Intro to Software Development; Subject: Computer Sci & Software Engr; University: Rose-Hulman Institute of Technology; Term: Unknown 1989;
Typology: Study notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!











win = GraphWin( 'Cubs Win!' , WIDTH, HEIGHT) p = Point(WIDTH/2, HEIGHT/2) t = Text(p, 'Chicago Cubs--National League Central Champs!' ) t.setStyle( 'bold' ) t.draw(win) nextColorIsRed = True t.setFill( 'blue' ) for i in range(REPEAT_COUNT): sleep(PAUSE_LENGTH) if nextColorIsRed: t.setFill( 'red' ) else : t.setFill( 'blue' ) nextColorIsRed = not nextColorIsRed win.close()
class Card: """This class represents a card from a standard deck."""
Declares a class named Card
docstring describes class, used by help() function and by Eclipse help
class Card: """This class represents a card from a standard deck.""" def init( self, card, suit): self.cardName = card self.suitName = suit
Special name, init declares a constructor
Special self parameter is the first formal parameter of each method in a class. self always refers to the current object
Create instance variables just by assigning to them
class Card: """This class represents a card from a standard deck.""" def init( self, card, suit): self.cardName = card self.suitName = suit
def getValue( self): """Returns the value of this card in BlackJack. Aces always count as one, so hands need to adjust to count aces as 11.""" pos = cardNames.index( self.cardName) if pos < 10: return pos + 1 return 10
def str( self): return self.cardName + " of " + self.suitName
Special str method returns a string representation of an object
class Card: """This class represents a card from a standard deck.""" def init( self, card, suit): self.cardName = card self.suitName = suit
def getValue( self): """Returns the value of this card in BlackJack. Aces always count as one, so hands need to adjust to count aces as 11.""" pos = cardNames.index( self.cardName) if pos < 10: return pos + 1 return 10
def str( self): return self.cardName + " of " + self.suitName
class CardCollection: """This class represents a collection of cards, either a single hand or the whole deck.""" def init( self, newDeck = False): """Creates a new collection of cards. By default it's empty, but if newDeck is True then the collection is a full standard deck.""" self.name = None self.cardList = [] self.hideFirst = True if newDeck: # Create an entire deck of cards for s in suits: for c in cardNames: self.insert(Card(c, s)) ...
Optional formal parameter , can construct a CardCollection three ways: deck = CardCollection(True) hand = CardCollection(False) hand = CardCollection()
Instance variable containing a list
Self call , constructor calls another method of the CardCollection class
class CardCollection: """…""" def init( self, newDeck = False): """…""" self.name = None self.cardList = [] self.hideFirst = True if newDeck: # Create an entire deck of cards for s in suits: for c in cardNames: self.insert(Card(c, s))
def insert( self, card): """Adds the given card to this collection.""" self.cardList.append(card)
insert method uses append() method to mutate the list instance variable
We can put objects into lists.