python的二十一点游戏

时间:2013-09-19 16:52:21

标签: python python-3.x blackjack

我正在为学校项目制作一个关于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我已略微编辑,现在得到这个。

1 个答案:

答案 0 :(得分:2)

在Python 3中print是一个函数。这意味着您必须使用括号print

>>> print '?'
  File "<stdin>", line 1
    print '?'
            ^
SyntaxError: invalid syntax
>>> print('!')
!