嗨我的朋友帮我在python中编写了这个游戏。但是我想在我的程序中添加一个分数计数器 - 分数计数器将跟踪用户输掉和获胜的次数。 如果你们可以告诉我如何在我的游戏中添加分数或告诉我如何真正欣赏它。我知道分数计数器应该是我主要功能的一部分,但我不知道如何添加分数计数器,因此我就在这里。
我正在使用PYTHON VERSION 2.7.6 几个月前开始编程的初学者总数:D 谢谢,这是我的代码:
import random
def chkValidInput (userInput):
chkNumber = False
for amtChar in range(0, len( userInput ) ):
for aNumber in range(0, 10):
if userInput[amtChar] == str(aNumber):
chkNumber = True
break
elif userInput[amtChar] != str(aNumber):
chkNumber = False
if chkNumber == False:
return False
return True
def evaluateBet(choice):
randNum = 0
randNum = random.randrange(1,14)
print "The random number is: " + str(randNum)
if choice == "high" and randNum > 7:
print "You Win!"
return True
elif choice == "low" and randNum < 7:
print "You Win!"
return True
else:
print "You Lose!"
return False
def main():
print "Commencing HiLo.py...\n"
random.seed(None)
points = 1000
wager = 0
userInput = None
programLoop = True
validInput = False
while programLoop == True:
print "Points remaining: " + str( points )
wager = 0
userInput = None
validInput = False
while validInput == False:
userInput = raw_input("\nMake a wager: ")
validInput = chkValidInput(userInput)
if validInput == False:
print "Invalid Input. It must be a positive Number. Try Again."
elif validInput == True:
if int( userInput ) > points:
validInput = False
print "You don't have enough points to make that wager. Try Again."
elif int( userInput ) == 0:
validInput = False
print "Your wager must be greater than 0. Try Again."
wager = int (userInput)
points = points - wager
print "Points remaining: " + str( points )
print "Wager: " + str( wager )
validInput = False
while validInput == False:
userInput = raw_input("\nPlace a bet: Either type 'High' or 'Low': ")
userInput = userInput.lower()
if userInput == "high" or userInput == "low":
validInput = True
else:
print "You may only choose to type from the options 'high' or 'low' "
if evaluateBet(userInput) == True:
points = ( points + (wager * 2) )
wager = 0
print "Points remaining: " + str( points )
validInput = False
if points <= 0:
print "You do not have anymore points; game over."
programLoop = False
else:
while validInput == False:
userInput = raw_input("\nWould you like to play again?: Type 'yes' or 'no': ")
userInput = userInput.lower()
if userInput == "yes" or userInput == "no":
validInput = True
else:
print "You may only choose to type from the options 'yes' or 'no' "
if userInput == "yes":
programLoop = True
elif userInput == "no":
programLoop = False
main()
答案 0 :(得分:1)
你已经拥有了制作分数计数器所需的几乎所有代码,只需要在这里和那里添加一些东西。
在def main()
下方定义wins
和losses
变量,均等于0
def main():
wins = 0
losses = 0
main()
函数
while programLoop == True:
将这些打印陈述放入,以便打印出分数
print "Wins: " + str(wins)
print "Losses: " + str(losses)
现在更改此if语句
if evaluateBet(userInput) == True:
points = ( points + (wager * 2) )
wager = 0
到
if evaluateBet(userInput) == True:
points = ( points + (wager * 2) )
wager = 0
wins += 1
else:
losses += 1
如果你正确添加这些代码行,你现在应该有一个功能分数计数器。
最后一件事,您的代码中存在缩进问题。
elif validInput == True:
if int( userInput ) > points:
validInput = False
print "You don't have enough points to make that wager. Try Again."
应该是
elif validInput == True:
if int( userInput ) > points:
validInput = False
print "You don't have
足以支付赌注。再试一次。&#34;
您只需向右移动validInput = False
个空格。
修改:完整代码
import random
def chkValidInput (userInput):
chkNumber = False
for amtChar in range(0, len( userInput ) ):
for aNumber in range(0, 10):
if userInput[amtChar] == str(aNumber):
chkNumber = True
break
elif userInput[amtChar] != str(aNumber):
chkNumber = False
if chkNumber == False:
return False
return True
def evaluateBet(choice):
randNum = 0
randNum = random.randrange(1,14)
print "The random number is: " + str(randNum)
if choice == "high" and randNum > 7:
print "You Win!"
return True
elif choice == "low" and randNum < 7:
print "You Win!"
return True
else:
print "You Lose!"
return False
def main():
wins = 0
losses = 0
print "Commencing HiLo.py...\n"
random.seed(None)
points = 1000
wager = 0
userInput = None
programLoop = True
validInput = False
while programLoop == True:
print "Wins: " + str(wins)
print "Losses: " + str(losses)
print "Points remaining: " + str( points )
wager = 0
userInput = None
validInput = False
while validInput == False:
userInput = raw_input("\nMake a wager: ")
validInput = chkValidInput(userInput)
if validInput == False:
print "Invalid Input. It must be a positive Number. Try Again."
elif validInput == True:
if int( userInput ) > points:
validInput = False
print "You don't have enough points to make that wager. Try Again."
elif int( userInput ) == 0:
validInput = False
print "Your wager must be greater than 0. Try Again."
wager = int (userInput)
points = points - wager
print "Points remaining: " + str( points )
print "Wager: " + str( wager )
validInput = False
while validInput == False:
userInput = raw_input("\nPlace a bet: Either type 'High' or 'Low': ")
userInput = userInput.lower()
if userInput == "high" or userInput == "low":
validInput = True
else:
print "You may only choose to type from the options 'high' or 'low' "
if evaluateBet(userInput) == True:
points = ( points + (wager * 2) )
wager = 0
wins += 1
else:
losses += 1
print "Points remaining: " + str( points )
validInput = False
if points <= 0:
print "You do not have anymore points; game over."
programLoop = False
else:
while validInput == False:
userInput = raw_input("\nWould you like to play again?: Type 'yes' or 'no': ")
userInput = userInput.lower()
if userInput == "yes" or userInput == "no":
validInput = True
else:
print "You may only choose to type from the options 'yes' or 'no' "
if userInput == "yes":
programLoop = True
elif userInput == "no":
print "Final wins and losses"
print "Wins: " + str(wins)
print "Losses: " + str(losses)
programLoop = False
main()