我对 python 很陌生,一段时间以来一直在尝试在 python 上制作多人二十一点游戏。我遇到了很多很多问题,想知道你们是否可以帮助我解决这些问题。
import random
def total(hand):
aces = hand.count(11)
t = sum(hand)
if t > 21 and aces > 0:
while aces > 0 and t > 21:
t -= 10
aces -= 1
return t
Cards = ["2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "AH", "JH", "QH", "KH", "AC", "JC", "QC", "KC", "AS", "JS", "QS", "KS", "AD", "JD", "QD", "KD"]
Cards[35] = 11
Cards[36] = 10
Cards[37] = 10
Cards[38] = 10
Cards[39] = 11
Cards[40] = 10
Cards[41] = 10
Cards[42] = 10
Cards[43] = 11
Cards[44] = 10
Cards[45] = 10
Cards[46] = 10
Cards[47] = 11
Cards[48] = 10
Cards[49] = 10
Cards[50] = 10
Players = raw_input("How many players are there?")
for i in range Players:
Player i = []
Player i.append(choice(Cards))
Player i.append(choice(Cards))
tp = total(player)
print "Player" + i + "Cards: " + Player i + "," + "total: " + tp
hitorstand = raw_input("hit (h) or stand (s)?")
if hitorstand == "h":
Player i.append(choice(cards))
print ("hit (h) or stand (s)?")
elif hitorstand == "s":
break
else print "Please enter h or s"
dealer = []
While True:
dealer.append(choice(cards))
dealer.append(choice(cards))
td = total(dealer)
while td > 17:
dealer.append(choice(cards))
else:
break
if td < tp < 21:
"Player i wins"
else print "dealer wins"
这就是我到目前为止所拥有的。我知道有很多乱码和代码不起作用。我想知道你们是否可以让我知道代码有什么问题,并可能建议一些有关如何修复它的选项。
我现在主要担心的是:
我正在制作“多人”二十一点游戏。我不知道我应该如何为多人二十一点游戏制作循环。在我的代码中,我问有多少人在玩。如何在不知道数字是多少的情况下为游戏制作循环?
另外,我如何创建一个函数来找出获胜者而不知道有多少玩家在玩?
我输入后
Players = raw_input("How many players are there?") for i in range Players:
Players
for 循环中的in 给了我一个语法错误。怎么了?
作为更新,我考虑了你所说的关于制作列表的内容,但我仍然不明白我应该如何制作代码来找出获胜者。
例如
即使我列出一个列表,如果我不知道实际有多少玩家在玩,我将无法比较列表中的元素。如果我知道有多少人在玩,
playerlist = [1,2,3]
我可以说
if playerlist[0] > playerlist[1], playerlist[2] and playerlist[0] < 21:
then print "player 1 wins!"
但是由于在用户实际输入输入之前我不知道有多少人在玩,所以我不知道应该如何为获胜者编写代码。
我不知道是否有一种说法“如果这比其他人大”。我只知道怎么说“如果这比那更大”。
有没有办法在python中说“如果这比其他的大”?如果没有,你能给我一些建议来制作代码来找出获胜者吗?