我正在编写一个玩纸牌游戏的程序。每个玩家都有一套卡片,我已经让它随机处理好了。我需要它能够比较列表中的两个值并根据它们的整数值做一些事情。我编写的代码如下:
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()
这是什么意思?比较前两个和其他两个有什么区别?