I was asked to write a "Rock Paper Scissors" game as a practice exercise for class. I ended up with this code which works as intended. It's a one player vs. the computer game.
Overview:
- Computer and player choose their 'weapons'
- Decide the winner
- Print the winner
Does anyone have any constructive feedback or tips on how I could improve this?
import random as r
#the possible moves in rock paper scissors
RPS = ["ROCK", "PAPER", "SCISSORS"]
def main():
comp = computerMove()
player = playerMove()
winner, tie = calcWinner(comp, player)
dispWinner(winner, tie)
#the move made by the computer
def computerMove():
comp = r.choice(RPS)
## print(comp)
return comp
#move made by the user
def playerMove():
player = ""
while not player in RPS:
player = input("Choose your weapon(rock, paper, or scissors: ").upper()
## print(player)
return player
#determining a winner
def calcWinner(comp, player):
print("The computer chooses {0}.".format(comp))
winner = None
tie = False
## if theres a tie
if player == comp:
print("No winner.... Restarting....\n")
main()
tie = True
## if the user wins
elif ((comp == "ROCK") and (player == "PAPER")) or ((comp=="PAPER") and (player == "SCISSORS")) or ((comp=="SCISSORS") and (player == "ROCK")):
winner = True
## if computer won
else:
winner = False
return winner, tie
def dispWinner(winner, tie):
if tie == True:
print("")
elif winner == True:
print("You beat the computer!")
elif winner == False:
print("The computer beat you!")
main()