Pseudocode array to Python list

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

Pseudocode array to Python list

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A pseudocode array is declared with DECLARE Scores : ARRAY[1:5] OF INTEGER. Which Python line creates a matching list?
A scores = [0] * 4
B scores = [0] * 5
2. In pseudocode, the first element of an array with lower bound 1 is Scores[1]. What is the same element in Python?
A scores[0]
B scores[1]
3. Which Python statement matches the pseudocode Scores[3] ← 70 (array lower bound 1)?
A scores[2] = 70
B scores[3] = 70
4. Pseudocode uses Scores[i] inside a loop. How is this written in Python so it works the same way?
A scores[i + 1]
B scores[i - 1]
5. The pseudocode loop is FOR i ← 1 TO 5. Which Python loop keeps the same i values and reaches every element?
A for i in range(1, 6):
B for i in range(1, 5):

Match the Pairs

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

Left columnRight column
1scores = [0] * 5 AThe pseudocode bounds 1 to 5 were copied directly, so scores[i] skips the first element and scores[5] is out of range on the last pass.
2scores[i - 1] BA list of 5 elements has indices 0 to 4, so scores[5] is out of range and causes an IndexError. The last element is scores[4].
3Pseudocode index 1 is the first element, but the first element of a Python list is at index 0 CCreates a Python list of 5 elements, all set to 0, to replace the pseudocode array Scores[1:5]
4A FOR loop from 1 to 5 over a pseudocode array becomes a Python loop over the indices 0 to 4 DPython's way of writing the pseudocode element Scores[i] when the array starts at index 1
5scores = [0] * 5 scores[5] = 90 Escores[0]
6for i in range(1, 6): total = total + scores[i] Ffor i in range(5):

Definitions

scores[i - 1]

Fill in the Blanks

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

1.
# ARRAY[1:10] OF INTEGER scores = [0] * __
2.
# Scores[1] in pseudocode print(scores[_])
3.
for i in range(1, 6): print(scores[i - _])
4.
# FOR i = 1 TO 5 for i in range(1, _): print(scores[i - 1])
5.
for i in range(1, 4): total = total + scores[i _ 1]

Spot the Error

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

Question 1
A scores = [0] * 5
B for i in range(1, 6):
C scores[i] = i * 10
D print(scores)
Question 2
A marks = [50, 60, 70, 80]
B first = marks[1]
C last = marks[3]
D print(first, last)
Question 3
A nums = [4, 8, 2, 6, 1, 9]
B for i in range(1, 6):
C print(nums[i - 1])
Question 4
A prices = [0] * 3
B prices[1] = 2.5
C prices[1] = 3.0
D prices[2] = 1.2
E print(prices)
Question 5
A scores = [0] * 4
B for i in range(1, 5):
C x = int(input())
D scores[i + 1] = x
E print(scores)

Answer Key

Pseudocode array to Python list
Section A: Quick Questions
Q1
B — scores = [0] * 5
Q2
A — scores[0]
Q3
A — scores[2] = 70
Q4
B — scores[i - 1]
Q5
A — for i in range(1, 6):
Section B: Match the Pairs
Q1
scores = [0] * 5 → Creates a Python list of 5 elements, all set to 0, to replace the pseudocode array Scores[1:5]
Q2
scores[i - 1] → Python's way of writing the pseudocode element Scores[i] when the array starts at index 1
Q3
Pseudocode index 1 is the first element, but the first element of a Python list is at index 0 → scores[0]
Q4
A FOR loop from 1 to 5 over a pseudocode array becomes a Python loop over the indices 0 to 4 → for i in range(5):
Q5
scores = [0] * 5 scores[5] = 90 → A list of 5 elements has indices 0 to 4, so scores[5] is out of range and causes an IndexError. The last element is scores[4].
Q6
for i in range(1, 6): total = total + scores[i] → The pseudocode bounds 1 to 5 were copied directly, so scores[i] skips the first element and scores[5] is out of range on the last pass.
Section C: Definitions
Q1
scores[i - 1] — A pseudocode array starting at index 1 becomes a Python list starting at 0, so element Scores[i] is written scores[i - 1] in Python.
Section D: Fill in the Blanks
Q1
# ARRAY[1:10] OF INTEGER scores = [0] * 10
Q2
# Scores[1] in pseudocode print(scores[0])
Q3
for i in range(1, 6): print(scores[i - 1])
Q4
# FOR i = 1 TO 5 for i in range(1, 6): print(scores[i - 1])
Q5
for i in range(1, 4): total = total + scores[i - 1]
Section E: Spot the Error
Q1
C — Pseudocode index i starts at 1, but Python lists start at 0, so the element is scores[i - 1]. Using scores[i] gives an IndexError when i is 5.
Q2
B — Marks[1] is the first element in pseudocode, which is index 0 in Python. marks[1] is the second element. It should be marks[0].
Q3
B — FOR i <- 1 TO 6 runs six times, but range(1, 6) stops at 5 and misses the last element. It should be range(1, 7).
Q4
B — Prices[1] in pseudocode is the first element, so in Python it is prices[0]. Writing prices[1] puts the value in the wrong place and it gets overwritten on the next line.
Q5
D — To convert a 1-based pseudocode index to a 0-based Python index you subtract 1, not add 1. It should be scores[i - 1].