More Arithmetic

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

More Arithmetic

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does import do?
A Loads a module so you can use it
B Prints text
2. Where should import go in your code?
A At the bottom
B At the top of the file
3. What does random.randint(1, 6) return?
A A random number from 1 to 6
B Always 1
4. What does time.sleep(3) do?
A Pauses for 3 seconds
B Prints 3
5. What happens if you use random.randint() without import?
A Returns 0
B NameError

Match the Pairs

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

Left columnRight column
1randint(1, 6) AOutput: 5.0
2import Random BError: Module names are lowercase
3import math print(math.pi) CError: Must import random first
4import random random.randint(1, 10) DOutput: 3
5import math print(math.sqrt(25)) EOk: Random integer 1-10
6import math print(math.floor(3.9)) FValid: Access math.pi

Definitions

import

Fill in the Blanks

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

1.
______ random print(random.randint(1, 10))
2.
import random print(random._______(1, 6))
3.
import ____ time.sleep(2)
4.
import random print(random.______(colors))
5.
import time time._____(3)

Spot the Error

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

Question 1
A import Random
B low = 1
C high = 10
D result = Random.randint(low, high)
E print(result)
Question 2
A import random
B low = 1
C high = 10
D result = randint(low, high)
E print(result)
Question 3
A import random
B low = 1
C high = 10
D result = random.randint(low)
E print(result)
Question 4
A import time
B print('Starting...')
C time.sleep('3')
D print('3 seconds later...')
E print('Done!')
Question 5
A import random()
B low = 1
C high = 6
D result = random.randint(low, high)
E print('Dice roll:', result)

Answer Key

More Arithmetic
Section A: Quick Questions
Q1
A — Loads a module so you can use it
Q2
B — At the top of the file
Q3
A — A random number from 1 to 6
Q4
A — Pauses for 3 seconds
Q5
B — NameError
Section B: Match the Pairs
Q1
randint(1, 6) → Error: Must import random first
Q2
import Random → Error: Module names are lowercase
Q3
import math print(math.pi) → Valid: Access math.pi
Q4
import random random.randint(1, 10) → Ok: Random integer 1-10
Q5
import math print(math.sqrt(25)) → Output: 5.0
Q6
import math print(math.floor(3.9)) → Output: 3
Section C: Definitions
Q1
import — Keyword that loads a module so you can use its functions.~~Module
Section D: Fill in the Blanks
Q1
import random print(random.randint(1, 10))
Q2
import random print(random.randint(1, 6))
Q3
import time time.sleep(2)
Q4
import random print(random.choice(colors))
Q5
import time time.sleep(3)
Section E: Spot the Error
Q1
A — Module name is lowercase: import random, not import Random
Q2
D — Must use dot notation random.randint(), not just randint()
Q3
D — randint() needs two arguments: the start and end numbers
Q4
C — sleep() takes a number, not a string: use 3, not '3'
Q5
A — Don't use parentheses with import: just import random