CSC111 Rock-Paper-Scissors Game

From dftwiki3
Revision as of 09:35, 14 February 2014 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Version 1= <br /> <source lang="python"> # rock-paper-scissors # rock1.py # D. Thiebaut # example of the use of if-else for playing the game # (this program is n...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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