Dec 28, 2009
Rock, Paper, Scissors Python Challenge
If you’ve have been following my blog lately, then you know that I have begun learning python. For the basics of Python I’ve been working my way through Core Python Programming 2nd edition by Wesley J. Chun. It’s a great book and I really enjoy reading it; my favorite parts are the exercises at the end of each chapter. These exercises are meant to reinforce the concepts presented throughout the chapters, but a few of the exercises are to challenge the reader. One such exercises I recently completed was the Rock, Paper, Scissors game. If your not familiar with Rock, Paper, Scissors consider yourself cheated out of a childhood (Here’s the Wikipedia article). I’m not going to cover my entire program just highlight a few key areas (The complete Source Code is located here). Also stick around I have a new challenge I’m working on to bring this game up to the next level.
Random Computer Weapon Selection
At first I thought I would need to assign numeric values to rock,paper, and scissors then randomly select a number using the randrange method. Then I discovered the choice method. This method randomly choices something from a list. All I had to do was create a list with rock, paper, and scissors and use the choice method to assign a variable containing the computer’s choice and I was good to go. Check it out below, and don’t forget to import random.
import random def computerChoice(): computerWeapons = ['Rock','Paper','Scissors'] computerWeapon = random.choice(computerWeapons) print 'Computer has selected %s' % computerWeapon return computerWeapon
Deciding Who Wins
I admit before I finished this exercise I looked at a few examples others have done; just to get a few ideas of course. Every example I could find had a large if/else block that was used to determine the winner. I knew their had to be an easier way, so I went back to basics. I broke the problem down into yes or no questions. For example if a player selected a rock I needed to know if the computer selected paper because that’s the only thing that could beat the player. By using this logic I first tested which weapon the player selected and then tested to see if the computer had a selected a weapon that could beat it. All of this fit nicely into 19 lines of code. A far better approach then the other examples I had found.
def determineWinner(playerWeapon,computerWeapon): if playerWeapon == computerWeapon: winner = 'Draw' else: if playerWeapon == 'Rock': if computerWeapon == 'Scissors': winner = 'Player' else: winner = 'Computer' elif playerWeapon == 'Paper': if computerWeapon =='Rock': winner = 'Player' else: winner = 'Computer' else: if computerWeapon == 'Paper': winner = 'Player' else: winner = 'Computer' return winner
Bring on the Next Challenge
To bring this game up a notch I challenged myself to use 25 weapons instead of the three traditional weapons. You can find a list of the outcomes for each weapon here. Just so you know there are 300 possible outcomes for the game. I think I have an easy way of determining a winner, but I would love to see what others come up with. So if you think you can solve this problem let me know how you do.







[...] This post was mentioned on Twitter by Robert V Bolton, GB7MBC. GB7MBC said: (Robert): Rock, Paper, Scissors Python Challenge: If you’ve have been following my blog lately, then you know tha http://url4.eu/10RMu [...]