top of page

Python tutorial 4: Import Random





In the previous posts we have looked at how to get started with writing a computer program in Python, and we also looked at loops and at storing information.


In this tutorial we are going to practise these concepts while looking at a new area of coding in Python which often comes in very useful: getting the computer to choose things randomly.


In our first tutorial we introduced the need to import modules. We will now import a module called random which has lots of tools to deal with random things, as you can guess from its name.


import random


We will be using two commands that are found in this module. The first one is random.randint(). which lets us create a random number. We can then store this number in a variable, as we have seen in our last tutorial, and print it out:



Line 3 asks the program to choose a random number between 1 and 10 and to store it using the name my_number. Line 5 prints it out. Try running the code above several times to see that a random number is chosen every time.


Now a bit of work for you to do: using the for loop we introduced in a previous tutorial, can you modify the program above so that the program prints out 25 different random numbers. Remember DRY - Don't Repeat Yourself.


Let us now look back at the code that we worked on in our last tutorial to draw a shape with a certain number of sides. Let's modify this so that the computer program chooses the number of sides randomly.


Compare this with the code from last time and you will spot one difference: on line 4, the variable number_of_sides now contains a random number between 3 and 18. Each time the program runs, the shape will have a different number of sides.


Let us add some code to write down the number of sides on the screen. We will do this by creating a new turtle whose job is simply to write text on the screen.




Try to explore what happens if you remove or change the code in lines 6 to 9. This is a great way of understanding what commands do.


Finally, we will also make the colour of the shape random. Let us first create a list of colours. In Python we can do this as follows:


colours = ["green", "blue", "purple", "orange", "turquoise", "salmon"]


Instead of random.randint, which chooses a random number, we will now use a different command from the random module: random.choice(). This command chooses an item at random from a list. So if we want to choose a random colour from the list we called colours above, we would need to write random.choice(colours).


Your task is to:

  1. Create the list of colours in your code

  2. Get the computer to choose a colour at random and store this colour in a variable. Let's call this variable chosen_colour. Remember that to store information in a variable you need to use the = sign

  3. Finally, what other change do you think you may need to do so that instead of always drawing in turquoise, the program uses whichever colour it chose at random?


Good luck. We'll give you the solution in our next tutorial.



If you enjoy our coding tutorials why not join us on a course, we have holiday, after school and flexible options available.





541 views

Comments


Python Coding for Young People

CT-Unlimited-FinalRAW-transparent.png

Codetoday Unlimited is for the curious teenager or preteen keen to learn proper Python coding. Stephen's courses start from the basics and carry on to intermediate and advanced levels.

Python Coding for Adults

The Python Coding Place is Stephen's platform full of courses and other resources for beginners and intermediate learners. The focus is on clarity and Stephen's unique communication style.

bottom of page