card – A basic playing card

A simple API for creating and using playing cards
print(f"Spades: {spades}, Hearts: {hearts}, Diamonds: {diamonds}, Clubs: {clubs}")
Spades: ♠, Hearts: ♥, Diamonds: ♦, Clubs: ♣
ERROR:root:No traceback has been produced, nothing to debug.

We will use numbers to represent playing cards clubs anbf ranks. These are the ranks:

suits
['♣', '♦', '♥', '♠']

For instance, the suit at number 0:

suits[0]
'♣'

These are the ranks:

ranks
[None, 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

For instance, the rank at index ‘1’ (note that there isn’t a card at position ‘0’ since we want the ranks to match the indeces where possible):

ranks[1]
'A'
Card(1,3)
3, ♦

Comparisson operators


source

Card.__eq__

 Card.__eq__ (a:__main__.Card)

Return self==value.

For instance, here is a tet of equallity…

test_eq(Card(suit=1, rank=3), Card(suit=1, rank=3) )

source

Card.__lt__

 Card.__lt__ (a:__main__.Card)

Return self<value.

and a test of <

assert Card(suit=1, rank=3) < Card(suit=2, rank=3)

source

Card.__gt__

 Card.__gt__ (a:__main__.Card)

Return self>value.

… and finaly of >:

assert Card(suit=3, rank=3) > Card(suit=2, rank=3)
assert not Card(suit=1, rank=3) > Card(suit=2, rank=3)

Here is an example of creating and displaying a card

c = Card(suit=1, rank=3)
c
3, ♦
print(c)
3, ♦

source

Card

 Card (suit:int, rank:int)

A playing card

Type Details
suit int An index into suits
rank int An index into ranks

Equality, less than, and greater than work on the rank and siuit indices:

c = Card(suit=1, rank=3)