Decks

Now that we have Card objects, the next step is to define a class to represent decks. Since a deck is made up cards, a natural choice is for each Deck object to contain a list of cards as an attribute. The following is a class definition for Deck. The init method creates the attribute cards and generates the standard set of fifty-two cards:

class Deck:
    """represents a standard Deck
       instance attributes: cards """
    def __init__(self):
        self.cards = []
        for suit in range(4):
            for rank in range(1,14)
            card = Card(suit,rank)
            self.cards.append(card)

The easiest way to populate the deck is with a nested loop. The outer loop enumerates the suits from 0 to 3. The inner loop enumerates the ranks from 1 to 13. Each iteration of the inner loop creates a new Card with current suit and rank, and appends it to self.cards.

Printing the deck

Here is a str method for Deck:

def __str__(self):
        temp = []
        for card in self.cards:
            temp.append(str(card))
        return ‘\n‘.join(temp)

The method demonstrates an efficient way to accumulate a large string, by building a list of strings and then using join. The built-in function str invokes the __str__ method on each card and returns the string representation. Since we invoke join on a newline character, the cards are separated by newlines.

........

from Thinking in Python

时间: 2024-09-30 23:27:54

Decks的相关文章

词组习语3057组

superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 1 Anointing of the Sick British English 2 Civvy Street Clerk of the Closet 3 I mean I must say 4 I suppose so I will thank you to do something 5 Incoming mail server Lithium battery 6 M

linux 各层调优必备工具

Brendan D. Gregg 维护了一份Linux性能调优工具的蓝图Linux Performance Tools,针对Linux系统的各个组件都有相应的分析工具,一目了然. Linux Performance Observability:sar, benchmarking, tuning:  Images license: creative commons Attribution-ShareAlike 4.0. This page links to various Linux perfor

英语电影剧本大全(中英对照)

目     录 <泰坦尼克号>全部英文剧本 TV REPORTER: Treasure hunter Brock Lovett is best known for finding Spanish gold off islands in the best Caribbean. LIZZY: It’s OK, I’ll get you in a minutes. Come on. TV REPORTER: Now he is using Russian subs to reach the most

codeforces Gym 100187J J. Deck Shuffling dfs

J. Deck Shuffling Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/J Description The world famous scientist Innokentiy continues his innovative experiments with decks of cards. Now he has a deck of n cards and k s

[uva 246][deque]10-20-30

 10-20-30  A simple solitaire card game called 10-20-30 uses a standard deck of 52 playing cards in which suit is irrelevant. The value of a face card (king, queen, jack) is 10. The value of an ace is one. The value of each of the other cards is the

matlab deckShuffling

% a script to determine how many perfect shuffles are required % before a given deck of cards returns to its original order clear all, close all % problem statement: % suppose we have a deck of 8 (distinct) cards % 1 2 3 4 5 6 7 8 % define a perfect

[ACM] SDUT 2883 Hearthstone II (第二类Stiring数)

Hearthstone II Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The new season has begun, you have n competitions and m well prepared decks during the new season. Each competition you could use any deck you want, but each of the decks must

SharePoint创建内容类型

SharePoint创建内容类型 内容类型的用途是多种多样的.创建一个内容类型也非常直接. 1. 点击网站操作--网站设置. 2. 点击网站内容类型,点击创建. 3. 命名Beginning_SharePoint_PPT,描述:Template for PPT decks.选择父内容类型(文档内容类型和文档),也可以创建新分类.点击确定. 4. 点击高级设置,可以给内容类型上传一个特定的模板,或者提供一个上传过的文档的链接.上载到文档库,然后添加URL,点击确定. 5. 此时创建一个新文档,或者

Monthly update for Dynamics 365 for Operation

日期 标题, 类别 版本 描述 2017/8/22 Dyn 365 Fin and Ops, Ent ed July 2017 Plat Update 10 Category: Download   This package contains the development VHD for Dynamics 365 for Operations July 2017 with Platform update 10. 2017/8/4 Finance and Operations, EE July