1

我正在尝试让这个扑克程序正常工作,这是我的第一个 Python 程序。我无法理解我遇到的两个错误,因为代码对我来说看起来是正确的。

使用 Eclipse/PyDev

> Traceback (most recent call last):
  File "C:\Documents and Settings\****\workspace\xfz\src\Unit1.py", line 98, in <module>
    print test()
 File "C:\Documents and Settings\****\workspace\xfz\src\Unit1.py", line 86, in test
    assert card_ranks(sf) == "TC 9C 8C 7C 6C"
AssertionError

编码:

def poker(hands):
    "Return the best hand: poker([hand,...]) => hand"
    return allmax(hands, key=hand_rank)

def allmax(iterable, key=None):
    "Return a list of all items equal to the max of the iterable."
    result, maxval = [], None
    key = key or (lambda x: x)
    for x in iterable:
        xval = key(x)
        if not result or xval > maxval:
            result, maxval = [x], xval
        elif xval == maxval:
            result.append(x)
    return result

def hand_rank(hand):
    ranks = card_ranks(hand)
    if straight(ranks) and flush(hand):
        return (8, max(ranks)) 
    elif kind(4, ranks):
        return (7, kind(4, ranks), kind(1, ranks)) # 99993 (7,9,3)
    elif kind(3, ranks) and kind(2, ranks):
        return (6, kind(3, ranks) and kind(2, ranks))
    elif flush(hand):
        return (5, ranks)
    elif straight(hand):
        return (4, ranks)
    elif kind(3, ranks):
        return (3, kind(3, ranks), ranks)
    elif two_pair(ranks):
        return (2, two_pair(ranks), ranks)
    elif kind(2, ranks):
        return (1, kind(2, ranks), ranks)
    else:
        return (0, ranks) 

def card_ranks(hand):
    "Return a list of the ranks, sorted with higher first."
    ranks = ['--23456789TJQKA'.index(r) for r,s in hand]
    ranks.sort(reverse=True)
    return [5, 4, 3, 2, 1] if (ranks == [14, 5, 4, 3, 2]) else ranks


def straight(ranks):
    "Return true if the ordered ranks form a 5 card straight"
    return (max(ranks)-min(ranks) == 4) and len(set(ranks)) == 5

def flush(hand):
    "Return True if all the cards have the same suit"
    suits = [s for r,s in hand]
    return len(set(suits)) == 1

def kind(n, ranks):
    for r in ranks:
        if ranks.count(r) == n: return r
    return None

def two_pair(ranks):
    pair = kind(2, ranks)
    lowpair = kind(2, list(reversed(ranks)))
    if pair and lowpair != pair:
        return (pair, lowpair)
    else:
        return None

def test():
    "Test cases for the functions in poker program"
    sf = "6C 7C 8C 9C TC".split() # Straight Flush
    fk = "9D 9H 9S 9C 7D".split() # Four of a Kind
    fh = "TD TC TH 7C 7D".split() # Full House
    tp = "5S 5D 9H 9C 6S".split()
    fkranks = card_ranks(fk)
    tpranks = card_ranks(tp)
    assert kind(4, fkranks) == 9
    assert kind(3, fkranks) == None
    assert kind(2, fkranks) == None
    assert kind(1, fkranks) == 7
    assert two_pair(fkranks) == None
    assert two_pair(tpranks) == (9, 5)
    assert card_ranks(sf) == "TC 9C 8C 7C 6C"
    assert card_ranks(fk) == "9D 9H 9S 9C 7D"
    assert card_ranks(fh) == "TD TC TH 7C 7D"
    assert poker([sf, fk, fh]) == sf
    assert poker([fk, fh]) == fk
    assert poker([fh, fh]) == fh
    assert poker([sf]) == sf
    assert poker([sf] + 99*[fh]) == sf
    assert hand_rank(sf) == (8, 10)
    assert hand_rank(fk) == (7, 9, 7)
    assert hand_rank(fh) == (6, 10, 7)

print test()
4

2 回答 2

1
    assert card_ranks(sf) == "TC 9C 8C 7C 6C"

评估为 False 因为

    card_ranks(sf) = [10, 9, 8, 7, 6]  != "TC 9C 8C 7C 6C" 

    >>> card_ranks(sf) 
    [10, 9, 8, 7, 6]
    >>> card_ranks(fk) 
    [9, 9, 9, 9, 7]
    >>> card_ranks(fh) 
    [10, 10, 10, 7, 7]

您运行的函数称为 test(),它使用 assert 进行测试以确保测试的语句为 True。如果其中任何一个为 False,则 Assert 会引发 AssertionError 并存在程序。

您的下一行也是 False :

    >>> poker([sf, fk, fh])
    [['6C', '7C', '8C', '9C', 'TC']]
    >>> sf
    ['6C', '7C', '8C', '9C', 'TC']
    >>> poker([sf, fk, fh]) == sf
    False

提示:在你的文件上运行 find/replace 并将 'assert' 替换为 'print'

于 2012-05-16T21:27:24.337 回答
0

改变:

assert card_ranks(sf) == "TC 9C 8C 7C 6C"
assert card_ranks(fk) == "9D 9H 9S 9C 7D"
assert card_ranks(fh) == "TD TC TH 7C 7D"

至:

assert card_ranks(sf) == [10, 9, 8, 7, 6]
assert card_ranks(fk) == [9, 9, 9, 9, 7]
assert card_ranks(fh) == [10, 10, 10, 7, 7]

因此,检查问题的一种简单方法是放置一些打印语句。从根本上说,看看你在做什么。您的 card_ranks() 函数在评论中说它返回一个列表,但您仍在将其与字符串进行比较。在编写代码之前多考虑一下。节省调试时间。

于 2012-05-16T21:33:22.177 回答