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(运算符,它能告诉你某样东 ...
随机推荐
- sublime text 3 安装注释
sublime text 3 1.安装Sublime Text 3 下载安装:http://www.sublimetext.com/3 Package Control安装:https://subli ...
- C#程序集编译输出XML文档的作用
下图是ClassLib1类库的项目属性 /// <summary> /// 读取INI文件 /// </summary> /// <param name="Se ...
- 黄聪:mysql 存在该记录则更新,不存在则插入记录的sql
一条mysql教程 存在该记录则更新,不存在则插入记录的sql , ‘yourname') ON DUPLICATE KEY UPDATE auto_name='yourname' ON DUPLIC ...
- 三. ServerSocket用法
一.构造ServerSocket 构造方法 (1)new ServerSocket( ) (2)new ServerSocket(int port,int backlog) (3)new Server ...
- linux 定时器编程实例(完善中).....
最近在写linux 下的定时器编程实验,测试发现 usleep函数在 x86 架构下的定时还是比较准确的,在arm9下 就不太准了. 今天用linux 下的setitimer()函数进行了定时 器的测 ...
- CMake使用教程
转自 RichardXG 原文 CMake使用教程 CMake是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目. 通过编写CMak ...
- c++学习-多态性
强制转换父类对象为子类 #include<iostream> #include<string> #include <typeinfo> using namespac ...
- eclipse的shell相关插件
1.Easy Shell a. 功能 可以在Eclipse IDE里选中一个文件或目录,利用Easy Sehll直接跳转到Sehll窗口,很方便 b. 安装 Help - Install New So ...
- 资源文件assets和 res下面raw文件的使用不同点
在建立项目中一般会默认建立assets文件,当然我们还可以在res文件下面建立raw文件夹,这里面都可以存放一些图片,音频或者文本信息,可以供我们在程序当中进行使用,不过他们两个也有不同点: asse ...
- SharedPreferences实现自动登录记住用户名密码
最近Android项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现. SharedPreferences简介 SharedPreferences ...