Runtime error: index out of range

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

Runtime error: index out of range

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A list has 5 items: scores = [4, 8, 6, 9, 7]. Which statement is true about scores[5]?
A It raises an IndexError because the last valid index is 4
B It returns 7 because it is the fifth item
2. Which expression gives the last valid index of a list called items?
A len(items) - 1
B len(items)
3. Which loop header causes an off-by-one IndexError when reading names[i]?
A for i in range(len(names) + 1):
B for i in range(len(names)):
4. Given names = [], what happens when the code runs print(names[0])?
A It prints None because the list is empty
B An IndexError is raised because an empty list has no index 0
5. Which check helps prevent an IndexError before using items[i]?
A if i <= len(items):
B if i >= 0 and i < len(items):

Match the Pairs

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

Left columnRight column
1items[len(items)] Aif 0 <= i < len(items): print(items[i])
2items[len(items) - 1] Bitems = [] print(items[0])
3Prevent an IndexError with a range check before using the index CGets the last item in the list safely, as long as the list is not empty
4Accessing the first item of an empty list causes an IndexError DRaises an IndexError, because the last valid index is len(items) - 1
5for i in range(len(items) + 1): print(items[i]) EA 3-item list only has indexes 0, 1 and 2, so index 3 is out of range. The last item is scores[2] or scores[-1].
6scores = [70, 85, 90] print(scores[3]) FOff-by-one error: the loop goes one step too far, so the last value of i equals len(items) and raises an IndexError. Use range(len(items)).

Definitions

IndexError

Fill in the Blanks

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

1.
items = [4, 8, 15, 16, 23] last = items[len(items) - _] print(last)
2.
items = ["a", "b", "c"] for i in range(___(items)): print(items[i])
3.
items = [] if len(items) _ 0: print(items[0])
4.
try: print(items[5]) except __________:
5.
if index >= 0 and index _ len(items): print(items[index])

Spot the Error

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

Question 1
A scores = [4, 8, 6, 9, 7]
B last = len(scores) - 1
C print(scores[last + 1])
Question 2
A names = ['Ava', 'Zoe', 'Leo']
B count = len(names)
C print('Names:', count)
D for i in range(count + 1):
E print(names[i])
Question 3
A items = [10, 20, 30, 40]
B total = 0
C i = 0
D while i <= len(items):
E total += items[i]
F i += 1
Question 4
A prices = [3, 5, 8]
B last = prices[len(prices)]
C print(last)
Question 5
A colours = ['red']
B print(colours[0])
C print(colours[1])

Answer Key

Runtime error: index out of range
Section A: Quick Questions
Q1
A — It raises an IndexError because the last valid index is 4
Q2
A — len(items) - 1
Q3
A — for i in range(len(names) + 1):
Q4
B — An IndexError is raised because an empty list has no index 0
Q5
B — if i >= 0 and i < len(items):
Section B: Match the Pairs
Q1
items[len(items)] → Raises an IndexError, because the last valid index is len(items) - 1
Q2
items[len(items) - 1] → Gets the last item in the list safely, as long as the list is not empty
Q3
Prevent an IndexError with a range check before using the index → if 0 <= i < len(items): print(items[i])
Q4
Accessing the first item of an empty list causes an IndexError → items = [] print(items[0])
Q5
for i in range(len(items) + 1): print(items[i]) → Off-by-one error: the loop goes one step too far, so the last value of i equals len(items) and raises an IndexError. Use range(len(items)).
Q6
scores = [70, 85, 90] print(scores[3]) → A 3-item list only has indexes 0, 1 and 2, so index 3 is out of range. The last item is scores[2] or scores[-1].
Section C: Definitions
Q1
IndexError — A runtime error raised when code uses an index that is not in the list, such as items[5] in a 5-item list. The last valid index is len(items) - 1.
Section D: Fill in the Blanks
Q1
items = [4, 8, 15, 16, 23] last = items[len(items) - 1] print(last)
Q2
items = ["a", "b", "c"] for i in range(len(items)): print(items[i])
Q3
items = [] if len(items) > 0: print(items[0])
Q4
try: print(items[5]) except IndexError:
Q5
if index >= 0 and index < len(items): print(items[index])
Section E: Spot the Error
Q1
C — The list has 5 items, so the last valid index is 4 (len(scores) - 1). Using last + 1 gives index 5, which raises an IndexError. It should be print(scores[last]).
Q2
D — This is an off-by-one error. range(count + 1) makes i reach 3, but the valid indexes are 0 to 2, so names[3] raises an IndexError. It should be range(count).
Q3
D — With <=, the loop runs when i equals 4, but the last valid index is 3. items[4] raises an IndexError. The condition should be i < len(items).
Q4
B — len(prices) is 3, but indexes only go up to 2. The last item is at len(prices) - 1. Without the - 1, the code raises an IndexError.
Q5
A — With only one item, the only valid index is 0. Then colours[1] on line 3 raises an IndexError. The list needs two items, 'red' and 'blue'.