Week 3 - Section 1 - Data types and a bit of gaming
In this section we will learn about lists and tuples and we will scratch the surface of pygame. Be prepared for excitement.
Chapter 4.4 - Random
4.4 - Random
Random is a function that creates random numbers. It is not in the core of Python but in a library (the random library). In order to use it first we have to import the library. Copy the following code to Thonny and save it as Randomize.
import random print (random.randrange(1,50))
If we wanted to randomly select a number between 50 and 80 then the code should be :
import random print(random.randrange(50,81))
You can explore random function and how to use it in several cases here
To combine lists with random we will create a list of 20 random numbers from 1 to 80 like in the kino game.
Copy the following code to Thonny's editor and save it as kino.
# This code creates and prints 20 kino numbers import random # random is a library kino_list= # create an empty list for i in range(20): kino_list.append(random.randrange(1,81)) # appends a random number to the list print (kino_list)
Are the numbers in the list unique?