Python Forum
Rock paper Scissors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock paper Scissors
#1
Hello team,

Any suggestion on how to improve my rps game before making a gui for it?

from random import randint

def startgame():
	print(
	''' What do you want to play:
		[0] Rock
		[1] Paper
		[2] Scissors
	''')

def inputValidation(userInput):

	while userInput not in [0, 1, 2]:
		print('Incorrect input, please insert a value between 0 and 2')
		userInput = int(input(prompt)) 
	return userInput 

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)

computerMove = randint(0, 2)
prompt = '> '

startgame()

userInput = int(input(prompt))
ValidatedInput = inputValidation(userInput)

result(ValidatedInput, computerMove)
Any thoughts are most welcome.
Reply
#2
Hello,

the code looks good to me.
It is common with this kind of games to have a while loop and let player replay if he wants, instead of rerunning the program. But that is up to you.
There are ways to shorten the part of code that checks for the winner. But in case of a simple game like RPS, the way you wrote it works fine and is readable.
Reply
#3
Thank you very much for your feed back

(Jan-31-2021, 11:29 AM)j.crater Wrote: Hello,

the code looks good to me.
It is common with this kind of games to have a while loop and let player replay if he wants, instead of rerunning the program. But that is up to you.
There are ways to shorten the part of code that checks for the winner. But in case of a simple game like RPS, the way you wrote it works fine and is readable.

Done that:

import sys
from random import randint

def startgame():
	print(
	''' What do you want to play:
		[0] Rock
		[1] Paper
		[2] Scissors

		[9] Quit
	''')


def inputValidation(userInput):

	while userInput not in [0, 1, 2, 9]:
		print('Incorrect input, please insert a value between 0 and 2')
		userInput = int(input(prompt)) 
	return userInput 

	

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 = '> '

c = True
while c:

	computerMove = randint(0, 2)

	startgame()

	userInput = int(input(prompt))

	if userInput == 9:
		sys.exit()

	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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code review of my rock paper scissors game Milan 0 2,013 May-25-2022, 06:59 AM
Last Post: Milan
  2nd Project: Rock, Paper, Scissors MiNigle 3 2,707 Jun-05-2020, 12:50 PM
Last Post: MiNigle
  Rock Paper Scissors Game NectDz 5 3,295 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