Rock, Paper, Scissors

In today's lesson we looked at random numbers and lists. Random numbers are useful for all sorts of things, including probability experiments, but they are particularly useful to add some unpredictability to games and simulations. If you missed the first assignment, click here for instructions on how to install Python. If you are unable to install Python then you can use an IDE such as replit.com to run your programs on-line.

Random Numbers in Python

Python doesn't have built-in support for random numbers, but it can be added easily by including a library. You can include the library by adding the following line of code - usually at the top of your program:

from random import randint

You can then use the randint() function in your program. It takes two numbers, which are the smallest and largest numbers that you want to generate - e.g. to simulate the roll of dice, you could use randint(1,6) to generate a number from 1 to 6.

Your Task

Your task is to use randomness to make a game. My idea is Rock, Paper, Scissors, but you could do something else if you prefer. Your program should:

If you choose to do Rock, Paper, Scissors, then I would approach the task using the following steps.  Start with number 1 and see how far you can get:

  1. Think about how you are going to represent the choices of rock, paper and scissors.  It will make things easier if you use the same method for the player the the "computer".  You might want to tell the player that the computer is choosing first so that they know the computer can't cheat.
  2. Get the computer to choose one of rock, paper or scissors (and store the choice as you decided in step 1).  You might like to tell the user that this step has taken place, but obviously not what the choice is!
  3. Ask the user for their choice.  Think about steps for making the choice easier to enter - think about reducing the amount of typing and/or the likelihood of mistakes, e.g. by using a menu or multiple-choice.
  4. Display the computer's choice.
  5. Announce the winner!

The last step is the most tricky, but if you can't get that working then don't let that stop you from submitting your assignment - the player can probably work out for themselves who has won.

Extension

You might like to use other things you've learnt in the programming course - e.g. you could offer the player a number of goes, use a variable to keep the score, etc. I have created a Fruit Machine if you would like to see how I've combined random numbers, lists, loops and decisions.