[英]What can I do to fix my Rock, Paper, Scissors code to keep score and to end the game
Here's my code for a Rock, Paper, Scissors game and I am confused on how to create a running score as well as a way to end the game once the user enters 0.这是我的石头剪刀布游戏代码,我对如何创建跑分以及在用户输入 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!" )
I want to also display the match winner once the user has typed 0 and the game has ended.一旦用户输入 0 并且游戏结束,我还想显示比赛获胜者。
Your loop is bad. 您的循环不好。
Remove : computerChoice = computerSelection()
and playerChoice = playerSelection()
because they are calculated in roundWinner
. 删除: computerChoice = computerSelection()
和playerChoice = playerSelection()
因为它们是在roundWinner
中计算的。 Your are actually doing 2 plays per loop. 您实际上每个循环执行2次播放。
Unindent : matchWinner(computerTotal, playerTotal)
Unindent: 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)
Also fix roundWinner
to return 'exit' in case player pressed 0. In that case, the exit decision should rise up to all higher functions. 还要修复roundWinner
在玩家按下0的情况下返回“退出”的情况。在这种情况下,退出决定应上升到所有更高的功能。
I've tried changing a bit your code and it didn't really go well, I'd suggest you to use less functions next time in cases like that, it will make your life easier. 我尝试过更改一下代码,但实际上并不能很好地执行,建议您下次在这种情况下使用较少的函数,这样会使您的生活更轻松。 Anyway, I took your code (with a few minor changes) and put it all in one function. 无论如何,我接受了您的代码(进行了一些小的更改)并将其全部放在一个函数中。
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!" )
As you can see, I didn't use a function to run the player Selection and I made it so whenever you enter 0 it will break the loop and show the results. 如您所见,我没有使用函数来运行播放器Selection,而是做到了,因此只要您输入0,它就会中断循环并显示结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.