Building A Bridge Hand Generator With Python Lesson 2
Using unicode
This is a continuation of an article that I began yesterday. The final article will contain 6 or 7 parts. We will be going through the creation of a program with python to created randomly selected bridge hands step by step. The first step was to import the shuffle module which we will be using to shuffle the deck of cards.
The next step is to add unicode symbols.
If you missed the first lesson rather than going back to it, I have created a draft version which at this point will have the first two lessons. I will be updating daily. It won’t be published until the series is complete. You can however view it here.
I wrote an article about using draft versions to provide more detail for images. From the link below scroll down to the picture of the young man dressed in green and click on the image.
To create the symbols for our bridge program we are going to by using unicode symbols.
♣ ♦ ♥ ♠
What are unicode symbols?
I wrote about unicode symbols in the following article:
Here is the code to produce the club symbol:
CLUBS = (u'\N{black club suit}')u'\N{named suit}' gives named unicode
'u\(hex number)' gives unnamed unicodeNumeric codes in unicode are 4 digit base 16 numbers. The codes for the card suits just happen to resemble the base 10 numbers that you are familiar with. A8B0 for example would be a legitimate hexadecimal number.
It is a python convention to use all caps for constants. CLUBS is prefered over clubs.
I could have used a numeric reference for the symbols hower when descriptive references are available it is much clearer to use them. For example
CLUBS = (u'\N{black club suit}')# is much clearer than
CLUBS = ('u\2663')The playing card symbols are available with a black center or with a white center. I could have used white centers to represent the red cards however I prefer the all black version.
I think that ♣ ♦ ♥ ♠ looks better than ♣ ♢ ♡ ♠Here is a link to the Unicode site
Here is the code for our program so far:
from random import shuffleCLUBS = (u'\N{black club suit}')
DIAMONDS = (u'\N{black diamond suit}')
HEARTS = (u'\N{black heart suit}')
SPADES = (u'\N{black spade suit}')Jim McAulay🍁 says: “When my daughter left for university I felt a great emptiness in my life, specifically in my checking account.”.

102–103






