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 replit.com 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:
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:
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.
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.