小编Ulq*_*aru的帖子

Python:'列表中的对象'检查和'__cmp__'溢出

这是我第一次出现堆栈溢出,所以如果格式不适合该网站,我很抱歉.我刚刚开始学习编程,差不多已经过去了2周.我正在从http://openbookproject.net/thinkcs/python/english3e/index.html学习python, 直到现在一切都很好,我刚刚被困了好几个小时.我google了很多但找不到合适的解决方案来解决我的问题所以我在这里.

正如CH17所解释的那样,我试图让OldMaidGame()运行没有问题.http://openbookproject.net/thinkcs/python/english3e/ch17.html - 大部分代码也来自前一章.

我发现的是我无法使用Deck.remove,Hand.remove_matches或任何其他类型的删除功能.经过一些调试后,我发现当程序检查卡片/手/等中是否存在给定卡片时会出现问题.它不可能匹配.然后经过一些回顾章节,(在第16章),我发现'如果卡在甲板/手/等:删除(卡)'等查找.cmp()的对象,以确定卡实际上是否存在于deck/hand/etc中.这是我在电子书给定代码上添加'ace'之后的cmp版本.

def __cmp__(self, other):
    """ Compares cards, returns 1 if greater, -1 if lesser, 0 if equal """
    # check the suits
    if self.suit > other.suit: return 1
    if self.suit < other.suit: return -1
    # suits are the same... check ranks
    # check for aces first.
    if self.rank == 1 and other.rank == 1: return 0
    if self.rank == 1 and other.rank != 1: return 1
    if self.rank …
Run Code Online (Sandbox Code Playgroud)

python list object cmp playing-cards

16
推荐指数
1
解决办法
1601
查看次数

标签 统计

cmp ×1

list ×1

object ×1

playing-cards ×1

python ×1