这是我的一个石头,布,剪刀游戏的代码,我很困惑如何创建一个运行分数,以及如何结束游戏,一旦用户输入0。你知道吗
import random
def main():
print ("Welcome to the Rock, Paper, Scissors Tournament!")
computerTotal = 0
playerTotal = 0
playerChoice = playerSelection()
while (playerChoice != 0):
computerChoice = computerSelection()
winner = roundWinner()
if (winner == 'computer'):
computerTotal = computerTotal + 1
elif (winner == 'player'):
playerTotal = playerTotal + 1
playerChoice = playerSelection()
matchWinner = (computerTotal, playerTotal)
def computerSelection():
choice = random.randint(1,3)
if choice == 1:
print ("Computer chose rock")
elif choice == 2:
print ("Computer chose scissors")
elif choice == 3 :
print ("Computer chose paper")
return choice
def playerSelection():
choice = input ("Enter 1 for rock, 2 for scissors, 3 for paper (0 to end the Tournament): ")
if choice == 0:
print ('Final score:', matchWinner() )
elif choice == 1:
print ('Player chose rock')
elif choice == 2:
print ('Player chose scissors')
elif choice == 3:
print ("Player chose paper")
return playerSelection
def roundWinner():
if playerSelection() == computerSelection():
print("Draw no one wins!")
elif playerSelection == 1 and computerSelection == 3:
print("Computer Wins!")
elif playerSelection == 1 and computerSelection == 2:
print("Player Wins!")
elif playerSelection == 3 and computerSelection == 1:
print("Player Wins!")
elif playerSelection == 3 and computerSelection == 2:
print("Computer Wins!")
elif playerSelection == 2 and computerSelection == 1:
print("Computer Wins!")
elif playerSelection == 2 and computerSelection == 3:
print("Player Wins!")
def matchWinner():
if computerTotal > playerTotal :
print (" Computer wins the game!" )
elif computerTotal == playerTotal:
print (" Draw no one wins!" )
elif computerTotal < playerTotal:
print (" Player wins the game!" )
当用户键入0并且游戏结束后,我还想显示比赛赢家。你知道吗
你的循环不好。你知道吗
删除:
computerChoice = computerSelection()
和playerChoice = playerSelection()
,因为它们是在roundWinner
中计算的。你的每一圈实际上要做2次。取消登录:
matchWinner(computerTotal, playerTotal)
同时修复
roundWinner
在播放器按0时返回'exit'。在这种情况下,退出决定应该上升到所有更高的职能部门。你知道吗我试过修改一下你的代码,但效果不太好,我建议你下次在这种情况下少用一些函数,这样会让你的生活更轻松。 不管怎样,我把你的代码(做了一些小改动)放在一个函数中。你知道吗
如您所见,我没有使用函数来运行播放器选择,因此每当您输入0时,它将中断循环并显示结果。你知道吗
相关问题 更多 >
编程相关推荐