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

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.

主站蜘蛛池模板: 潢川县| 台中县| 高州市| 通山县| 磴口县| 台州市| 名山县| 上犹县| 白朗县| 鄂托克前旗| 东丽区| 沛县| 古蔺县| 奇台县| 藁城市| 山阴县| 巴彦县| 广丰县| 吉首市| 区。| 太和县| 福建省| 元氏县| 麻江县| 宜宾县| 郎溪县| 苗栗市| 乳山市| 桦甸市| 石林| 白沙| 尼玛县| 蒙城县| 霸州市| 秦皇岛市| 汤原县| 大埔区| 逊克县| 潮州市| 和硕县| 江油市|