Python 巨蟒21点游戏
我正在为一个学校项目制作一个关于Python的21点游戏。我已经完成了游戏的主要部分,但我一直遇到语法错误。我试过调试它,但我搞不清楚出了什么问题 这是我的密码-Python 巨蟒21点游戏,python,python-3.x,blackjack,Python,Python 3.x,Blackjack,我正在为一个学校项目制作一个关于Python的21点游戏。我已经完成了游戏的主要部分,但我一直遇到语法错误。我试过调试它,但我搞不清楚出了什么问题 这是我的密码- def total(hand): aces = hand.count(11) t = sum(hand) if t > 21 and aces > 0: while aces > 0 and t > 21: t -= 10
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!"
def总计(手动):
得分=手数(11)
t=总和(手)
如果t>21且aces>0:
当aces>0和t>21时:
t-=10
ACE-=1
返回t
卡片=[2,3,4,5,6,7,8,9,10,10,10,10,11]
cwin=0
pwin=0
尽管如此:
玩家=[]
玩家。附加(rc(卡))
玩家。附加(rc(卡))
pbust=False
cbust=False
尽管如此:
tp=总计(玩家)
打印“玩家拥有这些卡%s,总价值为%d”%(玩家,tp)
如果tp>21:
打印”-->玩家被抓获了
pbust=True
打破
elif tp==21:
打印“\a BLACKJACK!!!”
打破
其他:
hs=原始输入(“击中或站立/完成(h或s):”)。下()
如果hs中的“h”:
玩家。附加(rc(卡))
其他:
打破
尽管如此:
comp=[]
公司附加(rc(卡片))
公司附加(rc(卡片))
尽管如此:
tc=总计(补偿)
如果tc<18:
公司附加(rc(卡片))
其他:
打破
打印“计算机共有%s个%d”%(组件,tc)
如果tc>21:
“打印”-->计算机坏了!”
cbust=True
如果pbust==False:
打印“玩家获胜!”
pwin+=1
elif tc>tp:
打印“电脑赢了!”
cwin+=1
elif tc==tp:
打印“平局!”
elif tp>tc:
如果pbust==False:
打印“玩家获胜!”
pwin+=1
elif cbust==False:
打印“电脑赢了!”
cwin+=1
打破
打印
打印“Wins,玩家=%d计算机=%d”%(pwin,cwin)
exit=原始输入(“按Enter键(q键退出):”)。lower()
如果退出时出现“q”:
打破
打印“谢谢你用电脑玩21点!”
我运行了3.3.2,我稍微编辑了一下,现在得到了这个
在Python 3中,print是一个函数。这意味着您必须将括号与
print
一起使用
>>> print '?'
File "<stdin>", line 1
print '?'
^
SyntaxError: invalid syntax
>>> print('!')
!
打印“?”
文件“”,第1行
打印“?”
^
SyntaxError:无效语法
>>>打印(“!”)
!
语法错误是什么?这是您的全部代码吗?哪里定义了
rc
?发布回溯(完整错误消息)。还可以在错误行之前发布几行代码,并给出错误行和行号。这并不总是准确的,因为真正的错误可能在代码中出现得更早,但这只是一个开始。我们需要看到它和周围的代码才能有意义地回答您的问题。@nasher99:我一直在试图获取相关细节,但您没有提供。它应该说的不仅仅是“无效语法”。另外,您使用的是什么版本的Python?请使用这些详细信息编辑您的问题。