2015-01-05 90 views
-1

這裏是絕對的初學者,試圖做一個簡單的投幣遊戲來練習我讀過的前幾章。我試圖給遊戲添加一個評分板,但得分始終停留在0到0.還有其他任何提示也會被讚賞。我不知道任何人的節目,所以我可以使用的建議。硬幣折騰分數爲零

"""This will be an attempt to create a simple 
coin toss game in which the player chooses heads or 
tails and a random generator will pick one from a list 
of either heads or tails and return whether or not 
the player chose correctly. I also hope to 
create a scoreboard""" 

from random import randint 

coin = ['heads' , 'tails'] 
side = coin[0] 
guess = 'heads' 
token = int(raw_input('how many games?')) 
wins = 0 
losses = 0 


def getinfo(token): 
    guess = raw_input('Heads or Tails?') 

def flipcoin(guess, side, wins, losses): 
    side = coin[randint(0,1)] 
    print 'You Chose: %s' %(guess) 
    print 'Coin Is: %s' %(side) 
    if guess == side: 
     print 'you win' 
     wins = wins + 1 
    else: 
     print 'you lose' 
     losses = losses + 1 

def main(token): 
    while token > 0: 
     getinfo(token) 
     flipcoin(guess, side, wins, losses) 
     if wins > losses: 
      print "You are winning %s to %d" %(wins , losses) 
     elif wins == losses: 
      print "You are tied %s to %d" %(wins , losses) 
     else: 
      print "You are losing %s to %d" %(losses, wins) 
     token -= 1 
     print '%d games left' %(token) 
     print 'wins: %s' %(wins) 
     print 'losses: %s' %(losses) 

main(token) 
+1

你的函數正在返回任何東西。 – Andy

+0

在'getinfo()'中將'main'中的'getinfo(token)'更改爲'guess = getinfo(token)'和'return raw_input('Heads or Tails?')'... –

+0

@Anthony Sidoti,我想因爲人們認爲你的問題不夠清楚,所以你會被拒絕投票。你的問題似乎對我很清楚(「爲什麼我的'wins'和'losses'變量停留在0?」),但你沒有明確說出。此外,StackOverflow似乎集體討厭「任何其他技巧」的請求;還有另一個董事會,http://codereview.stackexchange.com/,歡迎您提出一般要求檢查您的代碼。因此,StackOverflow上的用戶會低估你在這裏發佈代碼審查請求的懲罰。 – steveha

回答

1

你的問題是,變量在「模塊範圍」中聲明,該功能外,然後嘗試從內部對其進行修改功能。

而不是修改模塊範圍變量,這些函數創建自己的局部變量。

解決該問題的一種方法是爲變量添加global聲明。

Use of "global" keyword in Python

另一個,也許會更好,方法是將這些變量的一類,像這樣:

class GameState(object): 
    def __init__(self): 
     self.wins = 0 
     self.losses = 0 

state = GameState() 

# later, in code: 

state.wins += 1 # for a win 

全局變量使程序混亂和很難搞清楚。您可以通過將課程放在一起來改善課程。通過明確連接,您可以進一步提高程序的性能:將參數添加到您的函數中,並在類實例中傳遞要修改的函數,而不是在模塊級別有一個實例。

其他答案建議只是將變量移動到main()。這也是一種有效的方法,特別是對於像這樣的小而簡單的程序。我喜歡將所有變量組合到一個班級中,這是因爲我的經驗是隨着時間的推移,小程序會逐漸發展爲更大的程序。如果你將相關的東西放在一起,那麼擴大程序就更容易。但是對於這個程序,只需將變量放入main()就可以了。

0

您的「猜測,支持,勝利,損失」變量是全局變量,所以您不應該將它們傳遞給「flipcoin」函數。這樣做會創建具有相同名稱的新局部變量,並且全局變量永遠不會更新。

如果更改這些行,似乎工作:

def flipcoin(): 
    global wins, losses 
    side = coin[randint(0,1)] 
    print 'You Chose: %s' %(guess) 
    print 'Coin Is: %s' %(side) 
    if guess == side: 
     print 'you win' 
     wins = wins + 1 
    else: 
     print 'you lose' 
     losses = losses + 1 

def main(token): 
    while token > 0: 
     getinfo(token) 
     flipcoin() 
0

正如已經提到過的那樣,您應該返回您的功能。限制使用全局變量。

"""This will be an attempt to create a simple 
coin toss game in which the player chooses heads or 
tails and a random generator will pick one from a list 
of either heads or tails and return whether or not 
the player chose correctly. I also hope to 
create a scoreboard""" 

from random import randint 

coin = ['heads' , 'tails'] 

def getinfo(token): 
    return raw_input('Heads or Tails?') 

def flipcoin(guess, side, wins, losses): 
    side = coin[randint(0,1)] 
    print 'You Chose: %s' %(guess) 
    print 'Coin Is: %s' %(side) 
    if guess == side: 
     print 'you win' 
     wins = wins + 1 
    else: 
     print 'you lose' 
     losses = losses + 1 
    return side, wins, losses 

def main(): 
    token = int(raw_input('how many games?')) 
    wins = 0 
    losses = 0 
    side = coin[0] 
    guess = 'heads' 

    while token > 0: 
     guess = getinfo(token) 
     side, wins, losses = flipcoin(guess, side, wins, losses) 
     if wins > losses: 
      print "You are winning %s to %d" %(wins , losses) 
     elif wins == losses: 
      print "You are tied %s to %d" %(wins , losses) 
     else: 
      print "You are losing %s to %d" %(losses, wins) 
     token -= 1 
     print '%d games left' %(token) 
     print 'wins: %s' %(wins) 
     print 'losses: %s' %(losses) 

if __name__ == '__main__': 
    main() 
+0

,這讓我很感謝你 – tonysamurai