# File: Exercise6_1.py # Purpose: A program that plays the game of Paper, Scissors, Rock! # Author: Emily G. Allen (modified from code by D. Kumar) # CONSTANTS: # this way I don't have to keep track of which element in my list is which DRAW = 0 LOSE = 1 WIN = 2 # import the randrange function from random import randrange # Function: Does me beat you? If so, return True, False otherwise. def beats(me, you): if me == 'Paper' and you == 'Rock': # Paper beats rock return True elif me == 'Scissors' and you == 'Paper': # Scissors beat paper return True elif me == 'Rock' and you == 'Scissors': # Rock beats scissors return True else: False # Function: PlayGame # Description: main code for playing the game # Inputs: list of scores; # Outputs: list of scores def PlayGame(scores): # define items items = ['Rock', 'Paper', 'Scissors'] done = 'N' # set flag for stopping # Player and Computer make their selection... yourChoice = input('Please enter Rock, Paper, or Scissors: ') myChoice = items[randrange(0, 3)] # inform Player of choices print 'I picked', myChoice print 'You picked', yourChoice # Decide if it is a draw or a win for someone # update score count accordingly if myChoice == yourChoice: print 'We both picked the same thing.' print 'It is a draw.' scores[DRAW] = scores[DRAW] + 1 # use constants to access right index elif beats(myChoice, yourChoice): print 'Since', myChoice, 'beats', yourChoice, '...' print 'I win.' scores[LOSE] = scores[LOSE] + 1 else: print 'Since', yourChoice, 'beats', myChoice, '...' print 'You win.' scores[WIN] = scores[WIN] + 1 done = input('Are you done playing (Y/N): ') # note: I can put in some fancy string processing here to help # guarantee what the user enters is formatted correctly. For example done.capitalize() # incase they enter 'y' or 'n' if (done == 'Y'): return scores else: PlayGame(scores) # recurse # Main program def main():# Play Rock, Paper, Scissors! print 'Lets play Rock, Paper, Scissors!' # communicate w/user scores = [0,0,0] # initialize scoring list # play the game PlayGame(scores) print print '------------------------------------' print 'Score: Wins = ', scores[WIN], ' Loses = ', scores[LOSE], ' Draws = ', scores[DRAW] print 'Thank you for playing. Bye!' main() # invoke the program