gpt4 book ai didi

python - 代码执行中的小问题,python纸牌游戏模拟

转载 作者:太空宇宙 更新时间:2023-11-04 08:17:18 24 4
gpt4 key购买 nike

这可能与谁有关,

这是纸牌游戏模拟的一些代码。 ( war 游戏)。当我运行它时,我收到此错误:

player0.append(player1[range(warcard1 + 1)] 
^
SyntaxError: invalid syntax

我不知道错误是什么,但也许这里有人知道。希望有人能为我解决这个问题。当然,该代码仍在进行中,因此如果有任何其他错误,我很乐意听到它们及其可能的解决方案。

import random

cards = ['ace', 'ace', 'ace', 'ace', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10', 'jack', 'jack', 'jack', 'jack', 'queen', 'queen', 'queen', 'queen', 'king', 'king', 'king', 'king']

order = ['ace', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king']

print "shuffling cards"
random.shuffle(cards,random.random)
print "lets play"
player0 = [cards[i] for i in range(len(cards)) if i % 2 == 1]

player1 = [cards[i] for i in range(len(cards)) if i % 2 == 0]

while (len(player0) > 0 or len(player1) > 0):

nextcard0 = player0[0]
nextcard1 = player1[0]

cardplayed0 = order.index(nextcard0)

cardplayed1 = order.index(nextcard1)

if cardplayed0 > cardplayed1:

player0.append(nextcard0)
player0.append(nextcard1)
player0.remove(nextcard0)
player1.remove(nextcard1)

elif cardplayed0 < cardplayed1:

player1.append(nextcard1)
player1.append(nextcard0)
player1.remove(nextcard1)
player0.remove(nextcard0)

elif cardplayed0 == cardplayed1:
while warcardvalue0 == warcardvalue1:
if len(player0) >= 4:
warcard0 = 4
elif len(player0) < 4:
warcard0 = len(player0)

if len(player1) >= 4:
warcard1 = 4
elif len(player1) < 4:
warcard1 = len(player1)


warcardvalue0 = order.index(warcard0)
warcardvalue1 = order.index(warcard1)

if warcardvalue0 > warcardvalue1:
player0.append(player0[range(warcard0 + 1)]
player0.append(player1[range(warcard1 + 1)]
player0.remove(player0[range(warcard0 + 1)]
player1.remove(player1[range(warcard1 + 1)]

elif warcardvalue0 < warcardvalue1:
player1.append(player1[range(warcard1 + 1)]
player1.append(player0[range(warcard0 + 1)]
player1.remove(player1[range(warcard1 + 1)]
player0.remove(player0[range(warcard0 + 1)]
else
print "another war!"


if len(player1) == 0:
print "player1 won!"
elif len(player0) == 0:
print "player0 won!"

最佳答案

错误信息中指出的行

player0.append(player1[range(warcard1 + 1)] 
^

缺少结束符 )

事实上,所有这些行都缺少右括号:

       if warcardvalue0 > warcardvalue1:
player0.append(player0[range(warcard0 + 1)]
player0.append(player1[range(warcard1 + 1)]
player0.remove(player0[range(warcard0 + 1)]
player1.remove(player1[range(warcard1 + 1)]

elif warcardvalue0 < warcardvalue1:
player1.append(player1[range(warcard1 + 1)]
player1.append(player0[range(warcard0 + 1)]
player1.remove(player1[range(warcard1 + 1)]
player0.remove(player0[range(warcard0 + 1)]

您可能需要查看您的代码以确保没有其他实例出现此问题。

此外,您可能会考虑(并且肯定会受益)一个编辑器,该编辑器执行 paren matching为你,并会提醒你类似的问题。我用了一个,绝对值得。

如果您对此类编辑器感兴趣,可以从这里开始探索选项:Top 10 Best Text Editors With Brace Matching

关于python - 代码执行中的小问题,python纸牌游戏模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12083834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com