gpt4 book ai didi

python - 将一个列表中的项目移动到另一个列表中的纸牌游戏 Python

转载 作者:行者123 更新时间:2023-11-30 22:53:52 26 4
gpt4 key购买 nike

好吧,我正在尝试用 Python 制作扑克游戏..并且我想将用过的纸牌从列表移动到另一个列表,我将在其中存储用过的纸牌。我正在使用这段代码:

deck = ['AS', 'KS', 'QS', 'JS', '10S', '9S', '8S', '7S', '6S', '5S', '4S', '3S', '2S',\
'AD', 'KD', 'QD', 'JD', '10D', '9D', '8D', '7D', '6D', '5D', '4D', '3D', '2D',\
'AC', 'KC', 'QC', 'JC', '10C', '9C', '8C', '7C', '6C', '5C', '4C', '3C', '2C',\
'AH', 'KH', 'QH', 'JH', '10H', '9H', '8H', '7H', '6H', '5H', '4H', '3H', '2H']

used = []
p1 = []
p2 = []
a = 0

while (a < 2):
drawn_card = random.choice(deck)
deck.append(drawn_card)
deck = [f for f in deck if f not in used]
p1.append(drawn_card)
a+=1

# Don't read anything below this line

card1 = p1[0]
card2 = p1[1]
f_card1 = "Name/Suit"
f_card2 = "Name/Suit"

# Formatting Player1's first drawn card

if card1[0] == "A":
f_card1 = "Ace"
elif card1[0] == "K":
f_card1 = "King"
elif card1[0] == "Q":
f_card1 = "Queen"
elif card1[0] == "J":
f_card1 = "Jack"
else:
f_card1 = card1[0]


if card1[1] == "S":
f_card1 = f_card1 + " of Spades"
elif card1[1] == "D":
f_card1 = f_card1 + " of Diamonds"
elif card1[1] == "C":
f_card1 = f_card1 + " of Clubs"
else:
f_card1 = f_card1 + " of Hearts"

# Formatting Player1's second drawn card

if card2[0] == "A":
f_card2 = "Ace"
elif card2[0] == "K":
f_card2 = "King"
elif card2[0] == "Q":
f_card2 = "Queen"
elif card2[0] == "J":
f_card2 = "Jack"
else:
f_card2 = card2[0]


if card2[1] == "S":
f_card2 = f_card2 + " of Spades"
elif card2[1] == "D":
f_card2 = f_card2 + " of Diamonds"
elif card2[1] == "C":
f_card2 = f_card2 + " of Clubs"
else:
f_card2 = f_card2 + " of Hearts"

print(f_card1)
print(f_card2)

while 函数是我的问题所在。有时我打印卡片时会得到相同的卡片。我该如何解决这个问题以及我的错误在哪里?提前致谢

最佳答案

您只需将抽取的牌附加到您的已用列表中,而不是放回到牌组中:

used.append(drawn_card)   #instead of deck.append(..)

关于python - 将一个列表中的项目移动到另一个列表中的纸牌游戏 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38027527/

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