官术网_书友最值得收藏!

The Card class

The Card class will be the first class we define, as both of our other classes will need to use it. Open up a new file and type the following code:

import random

class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value

def __repr__(self):
return " of ".join((self.value, self.suit))

The only import we will need for our game is the random module. This will allow us to shuffle our virtual deck of cards at the beginning of every game.

Our first class will be one representing the playing cards. Each card will have a suit (hearts, diamonds, spades, and clubs) and a value (ace to king). We define the __repr__ function in order to change how the card is displayed when we call print on it. Our function will return the value and the suit, for example, King of Spades. This is all we need to do for a Card.

Next up, we need to create a Deck of these Card classes.

主站蜘蛛池模板: 北宁市| 留坝县| 沐川县| 当涂县| 莱阳市| 隆安县| 荔浦县| 阿坝县| 金沙县| 洪湖市| 乌拉特前旗| 南京市| 湘阴县| 海门市| 桃园市| 江永县| 图们市| 紫阳县| 威远县| 唐海县| 中方县| 屏东县| 桃源县| 天祝| 常宁市| 红河县| 繁峙县| 宽城| 观塘区| 新竹市| 阿鲁科尔沁旗| 高平市| 鸡东县| 正阳县| 尼勒克县| 定远县| 沂源县| 宜良县| 女性| 无极县| 黄陵县|