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

Decks的更多相关文章

  1. PHP执行文档操作

    1.POWINTPOINT系列 之前参与过一个商城的项目,里面有将excel 导出的功能,但是如果要弄成PPT的我们应该怎么办呢?PHP是属于服务器端的 总不能在里面装个Powintpoint吧.于是 ...

  2. venus

    The Venus system was a small timesharing system serving five or six users at a time:分时系统 The design ...

  3. Linux内核--网络栈实现分析(二)--数据包的传递过程(上)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7492423 更多请看专栏,地址 ...

  4. C语言学习002:第一个完整的C程序代码

    #include <stdio.h>//引用相关的外部库,stdio.h包含了终端读写数据的代码 //程序入口,程序通过main函数的返回值判断程序是否运行成功,0表示成功,非0表示程序运 ...

  5. 2106 Problem F Shuffling Along 中石油-未提交-->已提交

    题目描述 Most of you have played card games (and if you haven’t, why not???) in which the deck of cards ...

  6. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  7. Think Python - Chapter 18 - Inheritance

    In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...

  8. Codeforces Round #145 (Div. 2, ACM-ICPC Rules)

    A. Lefthanders and Righthanders \(i\)与\(i+\frac n2\)匹配,根据左右手调整位置. B. Reading 排序,取前\(k\)个. C. Weather ...

  9. c语言知识点总结(摘自head first c)

    gcc name.c -o name;   ./name或者gcc name.c -o name &&  ./name;同时执行关键字:void sizeof(运算符,它能告诉你某样东 ...

随机推荐

  1. 跟我一起写Makefile--- 变量(嵌套变量+追加变量+overrid+多行变量+环境变量+目标变量+模式变量)

    目录(?)[-] 使用变量 一变量的基础 二变量中的变量 三变量高级用法 四追加变量值 五override 指示符 六多行变量 七环境变量 八目标变量 九模式变量 使用变量 ———— 在Makefil ...

  2. CGI技术原理

    一.CGI技术 1.1 CGI的提出 CGI是外部扩展应用程序与WWW服务器交互的一个标准接口.按照CGI标准编写的外部扩展应用程序可以处理客户端(一般是WWW浏览器)输入的协同工作数据,完成客户端与 ...

  3. 黄聪:wordpress如何扩展TinyMCE编辑器,添加自定义按钮及功能

    在functions.php文件里面添加: add_action( 'admin_init', 'my_tinymce_button' ); function my_tinymce_button() ...

  4. Java heap dump触发和分析(转)

    为了分析java应用的内存泄漏,使用thread dump往往解决不了问题.使用jstat[eg:jstat-gcutil pid 1000 5]工具查看运行的java应用的heap size,per ...

  5. FreeDroid开发过程中遇到的一些问题

    http://bestzp.com/?p=83 Android Studio混淆: build.gradle中   1 2 3 4 5 6 buildTypes {         release { ...

  6. 玩转sublime(一)——玩转全局文件搜索/替换

    这个快捷键好记,一般的搜索是Ctrl+f,多了一个Shift就是全局搜索

  7. 最大熵的Java实现

    这是一个最大熵的简明Java实现,提供训练与预测接口.训练采用GIS训练算法,附带示例训练集.本文旨在介绍最大熵的原理.分类和实现,不涉及公式推导或其他训练算法,请放心食用. 最大熵理论 简介 最大熵 ...

  8. 键盘--android 隐藏系统键盘

    . -----------------------------------------已验证----------------------------------- public static void ...

  9. LPC1768之时钟

    一锁相环和CPU时钟. CPU时钟=锁相环0输出/CPU时钟配置寄存器的预分频值即:Fcpu=Fcco/CCLKCFG+1.锁相环可以把外部时钟倍频到较高频率,PLL0输出频率是: Fcco = (2 ...

  10. Yii 发送电子邮件

    yii 收发邮件 ------------------------------------------------------------------------------------------- ...