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
Decks的更多相关文章
- PHP执行文档操作
1.POWINTPOINT系列 之前参与过一个商城的项目,里面有将excel 导出的功能,但是如果要弄成PPT的我们应该怎么办呢?PHP是属于服务器端的 总不能在里面装个Powintpoint吧.于是 ...
- venus
The Venus system was a small timesharing system serving five or six users at a time:分时系统 The design ...
- Linux内核--网络栈实现分析(二)--数据包的传递过程(上)
本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7492423 更多请看专栏,地址 ...
- C语言学习002:第一个完整的C程序代码
#include <stdio.h>//引用相关的外部库,stdio.h包含了终端读写数据的代码 //程序入口,程序通过main函数的返回值判断程序是否运行成功,0表示成功,非0表示程序运 ...
- 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 ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
- Codeforces Round #145 (Div. 2, ACM-ICPC Rules)
A. Lefthanders and Righthanders \(i\)与\(i+\frac n2\)匹配,根据左右手调整位置. B. Reading 排序,取前\(k\)个. C. Weather ...
- c语言知识点总结(摘自head first c)
gcc name.c -o name; ./name或者gcc name.c -o name && ./name;同时执行关键字:void sizeof(运算符,它能告诉你某样东 ...
随机推荐
- 【freemaker】之自定义变量,特殊变量 globals ,循环对象取值
entity public class Employee { private Integer id; private String name; private Integer age; private ...
- C语言每日一题之No.7
今天是正式第一天在现有的世界里与自己相处,你再也没有另一个世界可以躲避了.终于要自己面对自己了,一个人要真实的面对自己的灵魂总是痛苦的.从学校到社会的环境转换,现实与理想的冲突,个人价值观和社会价值观 ...
- 【原】C# decimal字符串转成整数
第一种方法: string na="1000.53"; int a=int.Parse(na.Substring(0,na.IndexOf('.')));//返回值a=1000 第 ...
- Linux xargs命令
xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理.通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从 ...
- Hadoop学习5--配置本地开发环境(Windows+Eclipse)
一.导入hadoop插件到eclipse 插件名称:hadoop-eclipse-plugin-2.7.0.jar 我是从网上下载的,还可以自己编译. 放到eclipse安装目录下的plugins文件 ...
- java处理日期时间
java.util.Calendar Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR 等 日历字段之间的转换提供了一些方法,并为操作 ...
- $.getJSON 返回值、AJAX异步调用步骤
//首先要判断用户录入的手机号是不是中国移动的,不是直接给出提示,并终止登录 if($("#cmUsername1").val().isMobile())//手机号码 { jQue ...
- ArrayList源码
1.首先看对ArrayList的定义: public class ArrayList<E> extends AbstractList<E> implements Lis ...
- PLSQL_闪回操作2_Fashback Version Query
2014-12-09 Created By BaoXinjian
- js 神代码
eval(z='p="<"+"pre>"/* ,.oq#+ ,._, */;for(y in n="zw24l6k\ 4e3t4jnt4q ...