Python 3.x 什么是';内置函数或方法';对象不可下标错误';什么意思?
因此,我正在尝试编写一个好的ol'战争游戏,当我尝试将一个对象(一张牌)从一个列表(手)移动到另一个列表(手)时,我遇到了一个错误。有一些关于这方面的其他帖子,但我无法拼凑出该做什么。。。 代码如下:Python 3.x 什么是';内置函数或方法';对象不可下标错误';什么意思?,python-3.x,Python 3.x,因此,我正在尝试编写一个好的ol'战争游戏,当我尝试将一个对象(一张牌)从一个列表(手)移动到另一个列表(手)时,我遇到了一个错误。有一些关于这方面的其他帖子,但我无法拼凑出该做什么。。。 代码如下: import random cardvalues = {"ace" : 13 , "2" : 2 , "3" : 3 , "4" : 4 , "5" : 5 , "6" : 6, "7" : 7 , "8" : 8 , "9" : 9, "10" : 10 , "jack" : 11 , "que
import random
cardvalues = {"ace" : 13 , "2" : 2 , "3" : 3 , "4" : 4 , "5" : 5 , "6" : 6, "7" : 7 , "8" : 8 , "9" : 9, "10" : 10 , "jack" : 11 , "queen" : 12 , "king" : 13}
suits = {"clubs" : 1, "diamonds" : 2 , "spades" : 3 , "hearts" : 4}
deck = []
currentDeck = []
class card:
def __init__ (self, value, suit):
if value not in cardvalues:
raise RuntimeError("must input valid card value")...
###[此处的代码已被删除,因为它不会导致问题]
def battle():
if hand1[0] >= hand2[0]:
hand1.append[hand2.pop(0)] #The error happens in this function in the .append
print("player 1 won the battle")
elif hand1[0] < hand2[0]:
hand2.append[hand1.pop(0)]
print("player 2 won the battle")
else:
raise RuntimeError("something wrong")
while len(hand1) > 0:
while len(hand2) > 0:
battle()
if len(hand1) == 0:
print("PLAYER 2 WON THE WAR")
elif len(hand2) == 0:
print("PLAYER 1 WON THE WAR")
else:
print("no one won?")
def battle():
如果hand1[0]>=hand2[0]:
hand1.append[hand2.pop(0)]#此函数在.append中出错
打印(“玩家1赢得了战斗”)
elif hand1[0]0:
当len(hand2)>0时:
战斗()
如果len(hand1)==0:
打印(“玩家2赢得了战争”)
elif len(hand2)=0:
打印(“玩家1赢得了战争”)
其他:
打印(“没有人赢?”)
谢谢
我不仅需要帮助找到我的错误,而且它意味着什么 您在尝试调用
append时使用了[]
而不是()
您需要调用list.append()
,而不是尝试对其编制索引。将hand1.append[hand2.pop(0)]
替换为hand1.append(hand2.pop(0))
。从那往下的第三条线也是一样。