Classes

Your Assignment

Python is a classy language! <sad trombone>. Anyways, classes are a programming construct that makes your code much more reasonable to read, to use and to extend.

In this lab, I will ask that you create a Python class that represents a standard playing card.

Playing cards (in America, at least) have rank and suit.
  • The suits of a deck of playing cards are Hearts, Spades, Clubs, and Diamonds.
  • Cards of each suit range in rank from Ace (1), 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack (11), Queen (12) and King (13).

Additionally, when playing Blackjack, the values of each card is usually the same as its rank except that face cards (Jack, King, Queen) have a value of 10, and the Ace has a value of 1 (“Aces low”).

http://cliparts.co/cliparts/zcX/5BG/zcX5BGgbi.png

The modern set of playing cards. Courtesy of cliparts.co.

This assignment will ask you to implement a class representing a playing card, and name it PlayingCard. These class methods are required:

  1. __init__(self, rank, suit): This is your class initializer.

    • self is required, of course. Make sure you understand its purpose in classes.
    • rank is an int value that maps to the rank of a playing card. This number ranges from 1-13 (Ace, 2, 3, …, King.)
    • suit is a single character "d", "c", "h", "s" respectifully representing each suit a card could possibly have (Diamonds, Clubs, Hearts, or Spades)
  2. get_rank(self): When called on an instance of your

    PlayingCard class, it will return the rank of that particular card as an integer from 1-13 (Ace, 2, 3, …, King.)

  3. get_suit(self): When called on an instance of your

    PlayingCard class, it will return the suit of that particular card as single-character string of either "d", "c", "h", "s", where the letters represent Diamonds, Clubs, Hearts, or Spades, respectfully.

  4. bj_value(self): When called on an instance of your

    PlayingCard class, it will return the Blackjack value of that particular card as an integer.

  5. __repr__(self): You will be introduced to another dunder name. This

    one, when defined inside your class, allows any instance of your class to properly describe itself when you attempt to, say, print an object representing an instance of your class PlayingCard. Basically, it’s a technique to convert a non-printing object into a string.

  6. main() function that tests your PlayingCard class by creating n instances of randomly generated cards and then asking each instance of these cards to print their information and their Blackjack value.

    Note: You will print an instance of you PlayingCard object directly, making indirect (thus, proper) use of the __repr__ method!

Output

First, define your PlayingCard class, but don’t yet define main(). You can tell IDLE to load your program. Once your program is loaded, and you are at the “>>>” prompt, you can test your class yourself, directly:

>>> c = PlayingCard(1,"s")
>>> print(c)
Ace of Spades
>>>

Let’s try making a Queen of Clubs card:

>>> c2 = PlayingCard(12,"c")
>>> print(c2)
Queen of Clubs
>>> c2.bj_value()
10
>>> c2.get_rank()
12
>>> c2.get_suit()
'c'
>>>

Let’s see how your program is require to run, according to the assignment requirements, above. This is what your main() should do (printing the PlayingCard instance and it’s Blackjack value):

Testing card class
How many cards would you like to see? 5
Two of Hearts counts 2
King of Hearts counts 10
Ten of Spades counts 10
Five of Hearts counts 5
Ace of Diamonds counts 1

Submission Requirements

Your file is required to be named lab_classes.py.

Please read and follow the Submission Requirements to avoid point deductions specified below.

Grading

The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Note: There may be additional criteria for grading, this is just a summary of the major rubric items.

Grading Rubric
Type Description Point Loss (Max)
  [General]  
SyntaxError Exceptions A program that doesn’t even start due to syntax errors. -30
Docstrings Missing proper lab header (e.g. no name, lab section, etc) -10
main() Your program should have main() as the first-called function -10
PEP 8 Style Egregious problems with your code styling -10
Incorrect Filename Follow the submission instructions -5
Commenting Insufficient or excessive comments -5
  [Lab Specific]  
Classes You must use a Python class named PlayingCard to solve this lab assignment -50
Constructor Your class constructor should take single-letter codes for the card suit (‘d’, ‘c’, ‘h’, or ‘s’) -10
__repr__() Write a __repr__() method to allow your PlayingCard objects to return a string representation of iteself -5
Data Types Your functions need to return the correct data type -5/each