That Blue Square Thing

Python Programming - Rock, Paper, Scissors

I sat down with someone in registration to lend a hand with an implementation of the classic rock, paper, scissors game. I then threw an idea into a Year 9 class - specifically to use a while loop to allow score keeping.

So I thought I'd better do a worked example or three to show that I can actually code this sort of thing...

Basic RPS

This is simply one round without any bells or whistles. You enter your "hand" and then the computer's "hand" - this allows for easier testing of all the possible outcomes as you have total control. The result is a win, a loss or a tie.

The code is pretty simple. The one possible "wrinkle" is that I've used a quite complex if/elif/else structure with multiple conditions in place. This keeps my code shorter and easier to deal with in IDLE. The PDF document annotates the code to show you how it works (I hope).

PDF iconProgram file: Basic RPS - copy the code into IDLE

PDF iconAnnotated code for Basic RPS

RPS version 2 - keeping score

Part of the intention of the project was that you keep score with the first to 5 winning. I've changed that slightly as I didn't want a game to (potentially) carry on indefinitely if the players keep on tying. So I've set the game limit to be best to 5 but with a total of 10 rounds.

This uses a while loop. This is a slightly complex idea (and one with many potential issues with it by the way). But it essentially means that the game continues until someone gets to 5 or the total number of rounds played is 10.

A loop is a programming tool to keep on doing something - to loop through it a number of times. Sometimes we know the exact number of times we want to do something (there are 7 days in a week for example) and sometimes we don't. If we don't know then a while loop is usually a good choice (if we do know then we might use a for loop - but we'll save that for another day...).

To do this I need to use three variables - playerScore, computerScore and rounds. I add one to these whenever required - each round, when the player wins or when the computer wins (in the case of a tie no one gets a point but there's still a round happening). When any of the three variables gets to it's limit (5 for the scores or 10 for the number of rounds) then the while loop stops running.

Once again there is annotated code to help explain what I'm doing and how it works...

PDF iconProgram file: RPS version 2 (while loop) - copy the code into IDLE

PDF iconAnnotated code for RPS version 2

RPS version 3 - adding a function

Neither of my previous versions allowed the computer to randomly generate it's hand. That's OK but not great. So, then I added that functionality.

To do this I had to import the randint function from the random module. This allows me to generate a random number - in this case between 1 and 3. I can then use the random number to create either r, p or s.

A module is a set of useful functions (or bits of code) which someone else has written. By importing them we can use them to avoid having to write the code ourselves. This is normal in most modern programming languages and is a great timesaver!

I chose to do this in a function. Functions are sections of code that you may want to use multiple times or which are simply easier to write in one block to keep the program better organised. We need to define the function at the top of our code and then write the code to make it do it's job.

Then we can call the function. In this case I call it to assign the value the function produces to the variable compPlays - the "hand" the computer plays.

PDF iconProgram file: RPS version 3 (random computer hand) - copy the code into IDLE

PDF iconAnnotated code for RPS version 3

Function definitions should go at the top of your code. They have to be defined before they can be used (or called).