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.
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.
Your loop is bad.
Remove : computerChoice = computerSelection()
and playerChoice = playerSelection()
because they are calculated in roundWinner
. Your are actually doing 2 plays per loop.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.