這裏是絕對的初學者,試圖做一個簡單的投幣遊戲來練習我讀過的前幾章。我試圖給遊戲添加一個評分板,但得分始終停留在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)
你的函數正在返回任何東西。 – Andy
在'getinfo()'中將'main'中的'getinfo(token)'更改爲'guess = getinfo(token)'和'return raw_input('Heads or Tails?')'... –
@Anthony Sidoti,我想因爲人們認爲你的問題不夠清楚,所以你會被拒絕投票。你的問題似乎對我很清楚(「爲什麼我的'wins'和'losses'變量停留在0?」),但你沒有明確說出。此外,StackOverflow似乎集體討厭「任何其他技巧」的請求;還有另一個董事會,http://codereview.stackexchange.com/,歡迎您提出一般要求檢查您的代碼。因此,StackOverflow上的用戶會低估你在這裏發佈代碼審查請求的懲罰。 – steveha