0

我正在编写一个玩纸牌游戏的程序。每个玩家都有一套卡片,我已经让它随机处理好了。我需要它能够比较列表中的两个值并根据它们的整数值做一些事情。我编写的代码如下:

from random import *
def main():
    cards = [1,2,3,4,5,6,7,8,9,10,11,12]*4
    p1 = []
    p2 = []
    while len(cards) != 0:
        m = randint(0,len(cards))
        p1.append(cards[m-1])
        del cards[m-1]
        n = randint(0,len(cards))
        p2.append(cards[n-1])
        del cards[n-1]
    print(p1, p2)
    def game():
        if p1[0] > p2[0]:
            p1.append(p2[0])
            del p2[0]
        elif p2[0] > p1[0]:
            p2.append(p1[0])
            del p1[0]
        else:
            if len(p1) > len(p2):
                print(p1, p2)
                for i in range(1,len(p2)):
                    if int(p1[i]) > int(p2[i]):
                        p1.append(p2[0:i])
                        del p2[0:i]
                    if int(p2[i]) > int(p1[i]):
                        p2.append(p1[0:i])
                        del p1[0:i]
                    else:
                        continue
            else:
                print(p1, p2)
                for i in range(1,len(p2)):
                    if int(p1[i]) > int(p2[i]):
                        p1.append(p2[0:i])
                        del p2[0:i]
                    if int(p2[i]) > int(p1[i]):
                        p2.append(p1[0:i])
                        del p1[0:i]
                    else:
                        continue
    while len(p1) > 0 and len(p2) > 0:
         game()
    print("player 1 has", p1, " and player 2 has ", p2)
    if len(p1) == 0:
        print("Player 2 wins")
    elif len(p2) == 0:
        print("Player 1 wins")
    input("Press enter to exit")

但是每次我运行它时,它都可以正常运行,直到打成平手。一旦它比较除前两个以​​外的任何值,它就会打印此错误:

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    main()
  File "C:\Users\Jesse\Documents\Jesse\homework\Computer Science\Programs\War.py", line       52, in main
    game()
  File "C:\Users\Jesse\Documents\Jesse\homework\Computer Science\Programs\War.py", line      32, in game
     if p1[i] > p2[i]:
 TypeError: unorderable types: int() > list()

这是什么意思?比较前两个和其他两个有什么区别?

4

3 回答 3

2

好像您正在使用 Python3。Python2 将允许您比较 int 和 list,但它不是很有用,并且会掩盖您在此处遇到的错误

我想也许你的意思是在extend这里使用

                    p1.append(p2[0:i])

和这里

                    p2.append(p1[0:i])

代替append

于 2012-11-14T08:10:35.697 回答
1

一些杂项。提示(不过,您已经得到了直接问题的答案):

m = randint(0,len(cards))
p1.append(cards[m-1])
del cards[m-1]

你在这里为自己创造工作。模块中有一个方便的函数random称为randrange,这意味着您不必担心减去一个(顺便说一句,这可能意味着如果您得到 0,那么您将得到 -1,它是列表的最后一个元素,并导致问题(即,您正在修复甲板))...此外,lists 有一个方便的方法称为pop,它从列表中删除某个位置的元素,所以上面可能是:

p1.append(cards.pop(randrange(len(cards))))

但是,由于您正在处理卡片,因此有一个非常恰当地命名的方法(同样是随机的),称为shuffle

from random import shuffle

cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ,12] # * 4
shuffle(cards)
# [3, 4, 11, 9, 6, 2, 12, 5, 8, 1, 10, 7]

使用它,你可以做更少的“手工工作”,所以让我们来处理卡片......:

>>> p1hand, p2hand = cards[::2], cards[1::2]
>>> p1hand
[3, 11, 6, 12, 8, 10]
>>> p2hand
[4, 9, 2, 5, 1, 7]
于 2012-11-14T08:19:01.560 回答
0

您遇到的问题来自此行(及其变体):

p1.append(p2[0:i])

这样做是将列表中的一个切片附加p2p1. 也就是说,它添加一个新列表作为现有列表的成员。当您尝试在一个列表中的整数和另一个列表中的子列表之间进行比较时,这会导致问题。

相反,您想使用extendwhich 将切片的成员添加到另一个列表的末尾。

p1.extend(p2[0:i]) # note that the 0 here is not necessary!

这应该可以解决问题。

于 2012-11-14T08:11:44.993 回答