Python Forum
Code review of my rock paper scissors game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code review of my rock paper scissors game
#1
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.

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
Reply


Messages In This Thread
Code review of my rock paper scissors game - by Milan - May-25-2022, 06:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  GUI application - code review Sr999 3 679 Jan-06-2024, 10:14 PM
Last Post: Sr999
  Python code review | Tkinter gui application Sr999 19 2,042 Dec-03-2023, 10:24 PM
Last Post: menator01
  Rock paper Scissors Milan 2 2,737 Feb-01-2021, 03:42 PM
Last Post: Milan
  First time python user - Calculator code review Steamy 1 2,177 Jul-22-2020, 05:59 PM
Last Post: Larz60+
  2nd Project: Rock, Paper, Scissors MiNigle 3 2,680 Jun-05-2020, 12:50 PM
Last Post: MiNigle
  Rock Paper Scissors Game NectDz 5 3,247 May-31-2020, 12:40 PM
Last Post: NectDz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020