2014-12-03 23 views
-2

試圖製作基於分數的二十一點遊戲。我有它運行,除了比分計數,一切正常。它標誌着該輪後的點,但之後重置爲0。這裏是給我的部分問題Python如何保持分數的計數

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

這也許應該被移到http://codereview.stackexchange.com/。如果您有關於python的具體問題,請在此處查詢。問「爲什麼我的代碼無法正常工作」不是一個合適的問題。 – OozeMeister 2014-12-03 05:45:16

回答

0

試着改變你的變量全球:

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