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. Python编写一个Python脚本

    我想要一个可以为我的所有重要文件创建备份的程序.(下面测试环境为python2.7) 1.backup_ver1.py #!/usr/bin/python import os import time ...

  2. Avant Browser

    Avant Browser Avant 浏览器友好的用户界面为你的网络冲浪带来全新的效率和透明性.软件版本的不断升级使产品的可靠性稳步提高. 没有广告.没有恶意软件! Avant 浏览器是免费的.10 ...

  3. 36. Valid Sudoku

    ============= Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku b ...

  4. 黄聪:C#里如何使用WebBrowser获取处理AJAX生成的网页内容?

    等待网页执行完毕(AJAX执行后). 使用webBrowser1.Document.Body.OuterHtml可以获取到AJAX产生的网页内容.

  5. 理解Socket编程【转载】

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  6. spring2.5整合hibernate3.0

    第一步:配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  7. QRCode.jar生成二维码

    参考http://www.oschina.net/code/snippet_2252392_45457 package com.ORcode; import java.awt.image.Buffer ...

  8. 随笔http

    一个HTTP由一条请求命令和一个响应结果组成.这种通信是通过名为HTTP报文(message)的格式化数据块进行的.

  9. php 预定义接口

    Traversable Traversable { } 作用:检测一个类是否可以使用 foreach 进行遍历的接口. php代码中不能用.只有内部的PHP类(用C写的类)才可以直接实现Travers ...

  10. java学习日志(1):命令行and小程序

    1.dos命令行,常见的命令 dir:列出当前目录下的文件以及文件夹md:创建目录rd:删除目录(必须空)cd:进入指定目录cd.. :退回到上一级目录cd/:退回到根目录del:删除文件exit:退 ...