Card objects

There are fifty-two cards in a deck, each of which belongs to one of four suits and one of thirteen ranks. The suits are Spades, Hearts, Diamonds, and Clubs (in descending order in bridge). The ranks are Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King. Depending on the game that you are playing, an Ace may be higher than King or lower than 2.

If we want to define a new object to represent a playing card, it is obvious what the attributes should be: rank and suit. It is not as obvious what type the attributes should be. One possibility is to use strings containing words like ‘Spade’ for suits and ‘Queen’ for ranks. One problem with this implementation is that it would not be easy to compare cards to see which had a higher rank or suit.

An alternative is to use integers to encode the ranks and suits. In this context, ‘encode’ means that we are going to define a mapping between numbers and suits, or between numbers and ranks. This kind of encoding is not meant to be a secret.

For example, this table shows the suits and the corresponding integer codes:

Spades -> 3

Hearts -> 2

Diamonds -> 1

Clubs -> 0

This code makes it easy to compare cards; because higher suits map to higher numbers, we can compare suits by comparing their codes.

The class definition for Card looks like:

class Card:
    """ represents a standard playing card."""

    def __init__(self, suit=0, rank=2):
        self.suit = suit
        self.rank = rank

As usual, the init method takes an optional parameter for each attribute. The default card is the 2 of Clubs.

from Thinking in Python

时间: 2024-11-07 02:48:21

Card objects的相关文章

Class diagrams

So far we have seen stack diagrams, which show the state of a program, and object diagrams, which show the attributes of an object and their values. These diagrams represent a snapshot in the execution of a program, so they change as the program runs

[Django] ModelViewSet from rest_framework and Router

To build rest api easily, we can use ModelViewSet from rest_framework. It provides GET, POST, DELETE, PUT methods. from rest_framework.viewsets import ModelViewSet from .serializers import ListSerializer, CardSerializer from .models import List, Card

Class attributes

In order to print Card objects in a way that people can easily read, we need a mapping from the integer codes to the corresponding ranks and suits. A natural way to do that is with lists of strings. class Card: """ represents a standard pla

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. Th

我要翻译《Think Python》-002 贡献列表 & 目录部分

PDF源文件地址 :  http://www.greenteapress.com/thinkpython/thinkpython.pdf 贡献列表 自从本书诞生之后,有超过上百个目光敏锐且有想法的读者给我发来了许多建议并指出了一些需要修正的地方.他们的热情和无私的奉献给了我巨大的帮助.如果你有任何建议或者发现需要修正的地方,请发邮件至:[email protected].如果您的建议被采纳,您的大名将会出现在我们的贡献人员列表(除非你本人过于低调拒绝承认).如果您发现文中的错误内容,敬请提供一下

模型基本关系

一."一对多"关系 1.实现方式 通过在"多"方模型类中添加外键属性 ForeignKey class School(models.Model): #"一方"模型 name = models.CharField(max_length=30) address = models.CharField(max_length=30) history = models.TextField(null=True) class Student(models.Mod

python测试开发django-37.外键(ForeignKey)查询

前言 前面在admin后台页面通过设置外键,可以选择下拉框的选项,本篇主要讲解关于外键(ForeignKey)的查询 models设计 在上一篇的基础上新增一个BankName表,Card表通过外键关联到BankName class BankName(models.Model): '''银行信息''' bank_name = models.CharField(max_length=50, verbose_name="银行名称", default="") city =

python测试开发django-59.restful接口开发

前言 REST 不是什么具体的软件或者代码,而是一种思想.现在流行前后端分离开发项目,一般用 json 来交换数据. 相信写过模板的同学都知道,只要哪怕页面中的数据有一丝丝变动,那整个页面都需要重新渲染,这对性能无疑是巨大的浪费,并且页面中只有一些元素会和数据相联系, 比如列表中的 元素,如果数据有变化,能直接只更新 元素就好了,REST 就是为此而生. REST简介 什么是RESTful 面向资源? 先看REST是什么意思,英文Representational state transfer 表

CS300 P02 Matching Game

P02 Matching GameProgramming II (CS300) Fall 2019Pair Programming: ALLOWEDDue: 9:59PM on September 18thP02 Matching GameOverviewThis assignment involves developing a graphical application that represents a simple matchinggame with cards. Our game is