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
#2
import random

options = ("rock", "paper", "scissors")
player = None
Computer = random.choice(options)

while player not in options:
    player = input("Enter a Choise (rock, paper, scissors)")
    print("Kindly Chose in (rock, paper, scissors)")

print(f"Player: {player}")
print(f"Computer: {Computer}")

if player == Computer:
    print("That's a tie!")
elif player == "rock" and Computer == "scissors":
    print("You win!")
elif player == "paper" and Computer == "rock":
    print("You win!")
elif player == "scissors " and Computer == "paper":
    print("You win!")
else:
    print("You lose.")
Reply
#3
import random

options = ("rock", "paper", "scissors")
player = None
Computer = random.choice(options)

while player not in options:
    player = input("Enter a Choise (rock, paper, scissors)")
    print("Kindly Chose in (rock, paper, scissors)")

print(f"Player: {player}")
print(f"Computer: {Computer}")

if player == Computer:
    print("That's a tie!")
elif player == "rock" and Computer == "scissors":
    print("You win!")
elif player == "paper" and Computer == "rock":
    print("You win!")
elif player == "scissors " and Computer == "paper":
    print("You win!")
else:
    print("You lose.")
buran write Dec-02-2024, 07:56 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Feedback Requested: A Collection of Rock-Paper-Scissors Implementations JamzTyson 0 448 Jan-13-2025, 05:39 PM
Last Post: JamzTyson
  GUI application - code review Sr999 3 1,965 Jan-06-2024, 10:14 PM
Last Post: Sr999
  Python code review | Tkinter gui application Sr999 19 6,914 Dec-03-2023, 10:24 PM
Last Post: menator01
  Rock paper Scissors Milan 2 3,571 Feb-01-2021, 03:42 PM
Last Post: Milan
  First time python user - Calculator code review Steamy 1 2,871 Jul-22-2020, 05:59 PM
Last Post: Larz60+
  2nd Project: Rock, Paper, Scissors MiNigle 3 3,334 Jun-05-2020, 12:50 PM
Last Post: MiNigle
  Rock Paper Scissors Game NectDz 5 4,228 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