Random numbers with import random

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Random numbers with import random

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line must come at the top of your program before you can use random.randint()?
A include random
B import random
2. Which code gives a random whole number from 1 to 6, like rolling a dice?
A random.randint(1, 6)
B random.randint(6)
3. True or false: random.randint(1, 10) can return the number 10.
A False
B True
4. Which line correctly stores a random number in a variable?
A num = random.randint(1, 100)
B num = randint(1, 100)
5. Which code picks a random item from the list pets = ["cat", "dog", "fish"]?
A random.choice(pets)
B random.randint(pets)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1import random Aroll = random.randint(1, 6)
2random.randint(1, 6) Brandint is called without random. in front. It should be random.randint(1, 10).
3Roll a dice and save the random whole number from 1 to 6 in a variable CGives your program the random module so you can use random.randint() and random.choice()
4Pick a random item from a list of snacks DThe import random line is missing, so Python does not know what random is and shows a NameError. Add import random at the top.
5num = random.randint(1, 10) (no import random line at the top) Erandom.choice(snacks)
6import random num = randint(1, 10) FGives a random whole number from 1 to 6, including both 1 and 6

Definitions

import random

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ random
2.
import ______
3.
roll = random._______(1, 6)
4.
roll = random.randint(1, _)
5.
pick = random.______(colours)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import random
B dice = random.randint(1, 6)
C print('You rolled', dice)
D if dice = 6:
E print('Bonus roll!')
Question 2
A import randon
B num = random.randint(1, 10)
C print(num)
Question 3
A import random
B n = randint(1, 100)
C print('Number:', n)
Question 4
A import random
B pets = ['cat', 'dog', 'fish']
C pick = random.choose(pets)
D print('Your pet:', pick)
Question 5
A import random
B colours = ['red', 'blue']
C c = random.choice[colours]
D print(c)

Answer Key

Random numbers with import random
Section A: Quick Questions
Q1
B — import random
Q2
A — random.randint(1, 6)
Q3
B — True
Q4
A — num = random.randint(1, 100)
Q5
A — random.choice(pets)
Section B: Match the Pairs
Q1
import random → Gives your program the random module so you can use random.randint() and random.choice()
Q2
random.randint(1, 6) → Gives a random whole number from 1 to 6, including both 1 and 6
Q3
Roll a dice and save the random whole number from 1 to 6 in a variable → roll = random.randint(1, 6)
Q4
Pick a random item from a list of snacks → random.choice(snacks)
Q5
num = random.randint(1, 10) (no import random line at the top) → The import random line is missing, so Python does not know what random is and shows a NameError. Add import random at the top.
Q6
import random num = randint(1, 10) → randint is called without random. in front. It should be random.randint(1, 10).
Section C: Definitions
Q1
import random — Gives your program the random module. Then random.randint(1, 6) gives a random whole number from 1 to 6, and random.choice(my_list) picks a random item from a list.
Section D: Fill in the Blanks
Q1
import random
Q2
import random
Q3
roll = random.randint(1, 6)
Q4
roll = random.randint(1, 6)
Q5
pick = random.choice(colours)
Section E: Spot the Error
Q1
D — A single = assigns a value. To compare dice with 6 you need ==, so the line should be: if dice == 6:
Q2
A — The module name is spelled wrong. It must be random, so the line should be: import random
Q3
B — With just import random, randint must be called with the module name in front. It should be: n = random.randint(1, 100)
Q4
C — The function that picks a random item from a list is random.choice(), not random.choose(). It should be: pick = random.choice(pets)
Q5
C — Functions are called with round brackets (), not square brackets []. It should be: c = random.choice(colours)