我正在为一个学校项目在 Python 上制作二十一点游戏。我已经完成了游戏的主要部分,但我不断收到语法错误。我试图调试它,但我无法找出问题所在。
这是我的代码 -
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 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
cwin = 0
pwin = 0
while True:
player = []
player.append(rc(cards))
player.append(rc(cards))
pbust = False
cbust = False
while True:
tp = total(player)
print "The player has these cards %s with a total value of %d" % (player, tp)
if tp > 21:
print "--> The player is busted!"
pbust = True
break
elif tp == 21:
print "\a BLACKJACK!!!"
break
else:
hs = raw_input("Hit or Stand/Done (h or s): ").lower()
if 'h' in hs:
player.append(rc(cards))
else:
break
while True:
comp = []
comp.append(rc(cards))
comp.append(rc(cards))
while True:
tc = total(comp)
if tc < 18:
comp.append(rc(cards))
else:
break
print "the computer has %s for a total of %d" % (comp, tc)
if tc > 21:
print "--> The computer is busted!"
cbust = True
if pbust == False:
print "The player wins!"
pwin += 1
elif tc > tp:
print "The computer wins!"
cwin += 1
elif tc == tp:
print "It's a draw!"
elif tp > tc:
if pbust == False:
print "The player wins!"
pwin += 1
elif cbust == False:
print "The computer wins!"
cwin += 1
break
print
print "Wins, player = %d computer = %d" % (pwin, cwin)
exit = raw_input("Press Enter (q to quit): ").lower()
if 'q' in exit:
break
print "Thanks for playing blackjack with the computer!"
我运行 3.3.2 我稍微编辑了一下,现在得到了这个。