这是我的Rock,Paper,Scissors游戏代码,我对如何创建跑步成绩以及一旦用户输入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并且游戏结束后显示比赛获胜者。
答案 0 :(得分:0)
您的循环不好。
删除:computerChoice = computerSelection()
和playerChoice = playerSelection()
,因为它们是在roundWinner
中计算的。您实际上每个循环执行2次播放。
取消缩进:matchWinner(computerTotal, playerTotal)
while True:
winner = roundWinner()
if (winner == 'computer'):
computerTotal = computerTotal + 1
elif (winner == 'player'):
playerTotal = playerTotal + 1
elif (winner == 'exit'):
break
matchWinner(computerTotal, playerTotal)
还修复roundWinner
以在玩家按下0的情况下返回“退出”。在这种情况下,退出决定应上升到所有更高的功能。
答案 1 :(得分:-2)
我已经尝试过更改一下代码,但实际上并不太好,我建议您下次在这种情况下使用较少的函数,这样会使您的生活更轻松。 无论如何,我接受了您的代码(进行了一些小的更改)并将其全部放在一个函数中。
import random
def RPS():
computerTotal = 0
playerTotal = 0
playerChoice = input ("Enter 1 for rock, 2 for scissors, 3 for paper (0 to end the Tournament): ")
while (playerChoice != 0):
computerChoice = random.randint(1,3)
#Prints R/P/S of player & computer.
if computerChoice == 1:
print ("Computer chose rock")
elif computerChoice == 2:
print ("Computer chose scissors")
elif computerChoice == 3 :
print ("Computer chose paper")
if playerChoice == 1:
print ('Player chose rock')
elif playerChoice == 2:
print ('Player chose scissors')
elif playerChoice == 3:
print ("Player chose paper")
#Calculating & printing who won(the round)
if playerChoice == computerChoice:
print("Draw no one wins!")
elif playerChoice == 1 and computerChoice == 3:
print("Computer Wins!")
computerTotal = computerTotal + 1
elif playerChoice == 1 and computerChoice == 2:
print("Player Wins!")
playerTotal = playerTotal + 1
elif playerChoice == 3 and computerChoice == 1:
print("Player Wins!")
playerTotal = playerTotal + 1
elif playerChoice == 3 and computerChoice == 2:
print("Computer Wins!")
computerTotal = computerTotal + 1
elif playerChoice == 2 and computerChoice == 1:
print("Computer Wins!")
computerTotal = computerTotal + 1
elif playerChoice == 2 and computerChoice == 3:
print("Player Wins!")
playerTotal = playerTotal + 1
playerChoice = input ("Enter 1 for rock, 2 for scissors, 3 for paper (0 to end the Tournament): ")
#if we got here means the player entered 0. printing who won the game
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,它就会中断循环并显示结果。