Python如何保持得分记录

时间:2014-12-03 05:41:44

标签: python

尝试制作基于分数的二十一点游戏。我有它运行,一切正常,除了得分记录。它标志着该轮后的点,但在之后重置为0。这是给我提出问题的部分

pScore = dScore = 0

def play()
    while len(deck) >= 4:

        deal(deck, phand, dhand)

        moves(deck, phand, dhand, player, dealer)

        win(player, dealer, dhand, phand, pScore, dScore)

def win(player, dealer, dhand, phand, pScore, dScore): 
    player = sum(phand)
    dealer = sum(dhand)

    if player == 21 and dealer != 21:
        pScore += 1
        print "Player wins!"
    elif player < 21 and player > dealer:
        pScore += 1
        print "Player wins!"
    elif player <= 21 and dealer > 21:
        pScore += 1
        print "Player wins!"
    elif dealer == 21 and player != 21:
        dScore += 1
        print "Dealer wins!"
    elif dealer < 21 and dealer > player:
        dScore += 1
        print "Dealer wins!"
    elif dealer <= 21 and player > 21:
        dScore += 1
        print "Dealer wins!"
    elif player == dealer:
        print "Push!"

    print "\nPlayer's Score= ", pScore, "\nDealer's Score= ", dScore, "\n"

    return (pScore, dScore) 

1 个答案:

答案 0 :(得分:0)

尝试将变量更改为全局:

def win(player, dealer, dhand, phand, pScore, dScore):
    global pScore = global dScore = 0