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

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.

主站蜘蛛池模板: 玉林市| 汉源县| 江陵县| 阿拉尔市| 黑山县| 突泉县| 久治县| 丹寨县| 嘉鱼县| 胶南市| 百色市| 隆回县| 新安县| 兴隆县| 和硕县| 南郑县| 屯昌县| 繁昌县| 孝义市| 海原县| 石阡县| 抚松县| 景东| 宜章县| 宁陕县| 于田县| 正定县| 兴义市| 务川| 泌阳县| 板桥市| 盱眙县| 乌苏市| 麻阳| 林口县| 深圳市| 五指山市| 米脂县| 凤庆县| 衡水市| 吉林市|