In today's lesson we looked at random numbers. 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 Repl.it to run your programs on-line.
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 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:
be user-friendly - it should be obvious to the user what they need to do
correctly implement the rules of the game - the computer should behave as expected and shouldn't cheat
include random behaviour - it shouldn't do exactly the same thing every time
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.