May-25-2022, 06:59 AM
Hello team,
I would like to submit my rock paper scissors game for code review. Any suggestion on how to improve it is more than welcome.
I would like to submit my rock paper scissors game for code review. Any suggestion on how to improve it is more than welcome.
import sys
from random import randint
# Game instructions, should be displayed before every user input
def startgame():
print(
''' What do you want to play:
[0] Rock
[1] Paper
[2] Scissors
[9] Quit
''')
# Checks if input is between 0 and 2
def inputValidation(userInput):
while userInput not in [0, 1, 2, 9]:
print(errorMessage)
try:
startgame()
userInput = float(input(prompt))
except:
print(errorMessage)
startgame()
continue
if userInput == 9:
sys.exit()
if userInput in [0, 1, 2]:
break
return userInput
# Compares user's input with computer's input
def result(ValidatedInput, computerMove):
moves = {0:'Rock', 1:'Paper', 2:'Scissors'}
print(f'You played: {moves[ValidatedInput]}\nComputer played: {moves[computerMove]}')
w = 'You Win!'
l = 'You Lose!'
if ValidatedInput == computerMove:
print("It's a draw")
elif ValidatedInput == 0 and computerMove == 1:
print(l)
elif ValidatedInput == 0 and computerMove == 2:
print(w)
elif ValidatedInput == 1 and computerMove == 0:
print(w)
elif ValidatedInput == 1 and computerMove == 2:
print(l)
elif ValidatedInput == 2 and computerMove == 0:
print(l)
elif ValidatedInput == 2 and computerMove == 1:
print(w)
prompt = '> '
errorMessage = 'Incorrect input, please input one of the provided options'
#Game starts from here
while True:
computerMove = randint(0, 2)
while True:
try:
startgame()
userInput = int(input(prompt))
except:
print(errorMessage)
continue
else:
break
if userInput == 9:
break
ValidatedInput = inputValidation(userInput)
result(ValidatedInput, computerMove)
continue_input = input('Play again [y/n]? ').upper()
while continue_input not in 'YyNn':
print('Incorrect input, please type y or n')
continue_input = input((prompt))
if continue_input == 'N':
c = False
