# program to play rock, paper scissors # ECS 10, May 8, 2009 # STUB FOR TESTING -- TO BE REPLACED def getuser(): while True: n = input("Human: enter 0 (rock), 1 (paper), 2 (scissors), 3 (quit): ") if n < 0 or n > 3: print "Bad input; try again" else: break return n # STUB FOR TESTING -- TO BE REPLACED def getcomp(): while True: n = input("Computer: enter 0 (rock), 1 (paper), 2 (scissors), 3 (quit): ") if n < 0 or n > 3: print "Bad input; try again" else: break return n # function to determine who wins # parameters: user: user's selection # computer's selection # NOTE: 0 = rock, 1 = paper, 2 = scissors # returns: 0 if user won # 1 if computer won # 2 if tie # NOTE: caller depends on these values def whowins(user, comp): if user == comp: win = 2 elif (user, comp) == (1, 0) or (user, comp) == (2, 1) or (user, comp) == (0, 2): win = 0 else: win = 1 return win # pulling it all together # calls: getuser(), getcomp(), whowins() def main(): # initialize scores score = [ 0, 0, 0 ] # now we play while True: # get the user's choice userchoice = getuser(); if (userchoice == 3): break # get the computer's choice compchoice = getcomp(); # figure out who won winner = whowins(userchoice, compchoice) # announce it to the world! if winner == 0: print "You win" elif winner == 1: print "Computer wins" elif winner == 2: print "Tie" else: print "*** INTERNAL ERROR *** winner is", winner break # track the new score score[winner] += 1; print "You won", score[0], "games, the computer won", score[1], print "games, and", score[2], "games were tied" main()