Python TypeError:列表索引必须是整数,而不是列表 def轮流(玩家、手、玩家、堆、回合): 完成时(手)不正确: pile.append(手[player][0])#这一行 手[player].pop(0) 桩[-1]10: 打破 惩罚卡(球员、手牌、球员、牌堆、回合) 回程
由(#此行)标记的行返回标题中所述的错误,在我的程序中,我已将player设置为初始值等于0,因此应该没有问题,对吗Python TypeError:列表索引必须是整数,而不是列表 def轮流(玩家、手、玩家、堆、回合): 完成时(手)不正确: pile.append(手[player][0])#这一行 手[player].pop(0) 桩[-1]10: 打破 惩罚卡(球员、手牌、球员、牌堆、回合) 回程,python,python-3.x,Python,Python 3.x,由(#此行)标记的行返回标题中所述的错误,在我的程序中,我已将player设置为初始值等于0,因此应该没有问题,对吗 编辑:hands是一个列表列表,player是一个整数如果提供了一个工作代码示例,将更容易提供帮助。看起来有很多功能有副作用。也许您可以包括一些最小的模拟和下面这样的示例调用,以对问题进行一个包含的、可运行的演示 def take_turn(Nplayers, hands, player, pile, turn): while finished(hands) is not
编辑:hands是一个列表列表,player是一个整数如果提供了一个工作代码示例,将更容易提供帮助。看起来有很多功能有副作用。也许您可以包括一些最小的模拟和下面这样的示例调用,以对问题进行一个包含的、可运行的演示
def take_turn(Nplayers, hands, player, pile, turn):
while finished(hands) is not True:
pile.append(hands[player][0]) # this line
hands[player].pop(0)
while pile[-1] <= 10:
print(turn, ':', '\nPile:', pile, '\nHands\n', '\n'.join(map(str, hands)), '\n')
check_players(Nplayers, hands, player, pile, turn)
turn += 1
player = (player + 1) % Nplayers
if len(hands[player]) == 0:
hands.pop(player)
Nplayers -= 1
player = player % Nplayers
pile.append(hands[player][0])
hands[player].pop(0)
if table[-1] > 10:
break
penalty_card(Nplayers, hands, player, pile, turn)
return turn
def已完成(手动):
如果len(hands)==0:
返回真值
返回错误
def检查玩家(*args,**kwargs):
通过
def惩罚卡(*args,**kwargs):
通过
表=[9]
def轮流(球员、手、球员、桩、回合):
完成时(手)不正确:
pile.append(手[player][0])#这一行
手[player].pop(0)
桩[-1]10:
打破
惩罚卡(球员、手牌、球员、牌堆、回合)
回程
轮流(玩家=1,手=[1,1,1],[1,1],[1],玩家=0,桩=[5,5],回合=3)
你能显示回溯,以及调用掉头函数的代码吗?什么是hands
和player
?hands是一个列表列表,player是一个整数,代码有数百行长…@user2900785我猜你认为player
是一个整数,但它不是。检查调用此的代码。或者将打印类型(播放器)
放在要调试的行之前。
def finished(hands):
if len(hands) == 0:
return True
return False
def check_players(*args, **kwargs):
pass
def penalty_card(*args, **kwargs):
pass
table = [9]
def take_turn(Nplayers, hands, player, pile, turn):
while finished(hands) is not True:
pile.append(hands[player][0]) # this line
hands[player].pop(0)
while pile[-1] <= 10:
print(turn, ':', '\nPile:', pile, '\nHands\n', '\n'.join(map(str, hands)), '\n')
check_players(Nplayers, hands, player, pile, turn)
turn += 1
player = (player + 1) % Nplayers
if len(hands[player]) == 0:
hands.pop(player)
Nplayers -= 1
player = player % Nplayers
pile.append(hands[player][0])
hands[player].pop(0)
if table[-1] > 10:
break
penalty_card(Nplayers, hands, player, pile, turn)
return turn
take_turn(Nplayers=1, hands=[[1,1,1], [1,1], [1]], player=0, pile=[5, 5], turn=3)