0
def run_counter(card_list, num_cards):
    hand = functions1.hand_sample(card_list, num_cards)
    hand1 = functions1.hand_shuffle(card_list, num_cards)
    srt_hand = sorted(hand)
    srt_hand1 = sorted(hand1)
    prop_hand = functions1.proper_hand(srt_hand)
    prop_hand1 = functions1.proper_hand(srt_hand1)
    run1 = 0
    i = 0
    for count in range(len(srt_hand)-1):
        if srt_hand[i] == srt_hand[i+1] - 1:
            run1 += 1
    run2 = 0
    i = 0
    for count in range(len(srt_hand1)-1):
        if srt_hand1[i] == srt_hand1[i+1] - 1:
            run2 += 1
    return run1+1, run2+1, prop_hand, prop_hand1

I have a hand of cards that I generate, and store in a sorted list 'srt_hand' and 'srt_hand1'. I made a function that should count the length of the runs in the hand. I go to test it but it keeps saying that 'if srt_hand[i] == srt_hand[i+1] - 1:' is an unsupported operand type. Why is that? It doesn't make any sense since I'm indexing a list of integers...

I had this before, with the same sorting technique and it worked (except it stopped after the first run):

run1 = 0
i = 0
while i < len(srt_hand)-1 and run1 < 2:
    while i < len(srt_hand)-1 and srt_hand[i] == srt_hand[i+1] - 1:
        if srt_hand[i] == srt_hand[i+1]:
            i += 1
        run1 += 1
        i += 1
    i += 1
run2 = 0
i = 0
while i < len(srt_hand1)-1 and run2 < 2:
    while i < len(srt_hand1)-1 and srt_hand1[i] == srt_hand1[i+1] - 1:
        if srt_hand[i] == srt_hand[i+1]:
            i += 1
        run2 += 1
        i += 1
    i += 1
return run1+1, run2+1, prop_hand, prop_hand1
4

1 回答 1

0

sorted函数在 python3.3 中返回一个生成器,因此您不能像列表那样对它进行索引srt_hand[i](这将引发异常)。

于 2013-11-15T02:07:27.933 回答