CSC111 Rock-Paper-Scissors Game
--D. Thiebaut (talk) 09:35, 14 February 2014 (EST)
Version 1
# rock-paper-scissors
# rock1.py
# D. Thiebaut
# example of the use of if-else for playing the game
# (this program is not very robust and will not work
# if the user does not enter an uppercase letter that is
# R, P, or S.
#
from random import choice
#--- constants ----
OPTIONS = [ 'R', 'P', 'S' ]
USERWINS = "You win!"
COMPUTERWINS = "I win!"
#--- computer picks a letter ---
computer = choice( OPTIONS )
#print( computer )
#--- user picks a letter ---
human = input( "Your play? " )
print( "Your play: %s Computer Play: %s" % ( human, computer ) )
#--- decide who wins ---
if human==computer:
print( "It's a tie!" )
else:
if human == 'P':
# user plays Paper
if computer == 'S':
# computer plays Scissors
print( COMPUTERWINS )
else:
# computer plays 'R'
print( USERWINS )
else:
if human == 'R':
# user plays Rock
if computer == 'S':
#computer plays Scissors
print( USERWINS )
else:
# computer plays Paper
print( COMPUTERWINS )
else:
# user has played Scissors
if computer == 'R':
# computer plays Rock
print( COMPUTERWINS )
else:
# computer plays Paper
print( USERWINS )
Version 2
This is a tighter version. Shorter and still efficent.
# rock-paper-scissors
# rock1.py
# D. Thiebaut
# example of the use of if-else for playing the game
# (this program is not very robust and will not work
# if the user does not enter an uppercase letter that is
# R, P, or S.
#
from random import choice
OPTIONS = [ 'R', 'P', 'S' ]
USERWINS = "You win!"
COMPUTERWINS = "I win!"
computer = choice( OPTIONS )
print( computer )
human = input( "Your play? " )
print( "Your play: %s Computer Play: %s" % ( human, computer ) )
if human==computer:
print( "It's a tie!" )
elif human == 'P' and computer =='R':
print( USERWINS )
elif human == 'P' and computer =='S':
print( COMPUTERWINS )
elif human == 'R' and computer =='S':
print( USERWINS )
elif human == 'R' and computer =='P':
print( COMPUTERWINS )
elif human == 'S' and computer =='P':
print( USERWINS )
#elif human == 'S' and computer =='R':
else:
print( COMPUTERWINS )
Version 3
Even more tight, and still very efficient.
# rock-paper-scissors
# rock1.py
# D. Thiebaut
# example of the use of if-else for playing the game
# (this program is not very robust and will not work
# if the user does not enter an uppercase letter that is
# R, P, or S.
#
from random import choice
OPTIONS = [ 'R', 'P', 'S' ]
USERWINS = "You win!"
COMPUTERWINS = "I win!"
computer = choice( OPTIONS )
print( computer )
human = input( "Your play? " )
print( "Your play: %s Computer Play: %s" % ( human, computer ) )
if human==computer:
print( "It's a tie!" )
elif human == 'P' and computer =='R' \
or human == 'R' and computer =='S' \
or human == 'S' and computer =='P':
print( USERWINS )
else:
print( COMPUTERWINS )