Turning pseudocode into Python

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

Turning pseudocode into Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which Python line matches the pseudocode: score <- 0
A score = 0
B score <- 0
2. Which Python line matches the pseudocode: INPUT name
A name = input()
B input name
3. Which Python line matches the pseudocode: IF score > 10 THEN
A if score > 10 then
B if score > 10:
4. Which Python line matches the pseudocode: FOR i <- 1 TO 5
A for i in range(1, 5):
B for i in range(1, 6):
5. How does Python show END IF from your pseudocode?
A By un-indenting the next line
B By typing end if

Match the Pairs

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

Left columnRight column
1score <- 0 Afor i in range(1, 6):
2name = input("Name? ") Bif score > 5:
3Pseudocode IF score > 5 THEN becomes an if line ending with a colon CThe pseudocode arrow <- was copied into Python. Python needs = instead: score = 0
4Pseudocode FOR i <- 1 TO 5 becomes a for loop with range(1, 6) DThe if line is missing its colon, and the print line is not indented. END IF is shown by un-indenting, not by a keyword.
5score <- 0 EPseudocode assignment arrow: in Python it becomes score = 0
6if age >= 13 print("Teen") FPython version of the pseudocode step INPUT name

Definitions

score <- 0 becomes score = 0

Fill in the Blanks

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

1.
score _ 0
2.
name = _____("Your name? ")
3.
__ score >= 10: print("You win!")
4.
for i in _____(1, 6): print(i)
5.
if age >= 12 ___ age <= 15: print("Teen")

Spot the Error

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

Question 1
A score = 0
B name <- input('Name: ')
C print(name)
Question 2
A score <- 0
B if score > 5:
C print('Win')
Question 3
A for i in range(5)
B print(i)
C print('Done')
Question 4
A age = int(input('Age: '))
B if age => 13:
C print('Teen')
Question 5
A total = 0
B for n range(1, 4):
C total = total + n
D print(total)

Answer Key

Turning pseudocode into Python
Section A: Quick Questions
Q1
A — score = 0
Q2
A — name = input()
Q3
B — if score > 10:
Q4
B — for i in range(1, 6):
Q5
A — By un-indenting the next line
Section B: Match the Pairs
Q1
score <- 0 → Pseudocode assignment arrow: in Python it becomes score = 0
Q2
name = input("Name? ") → Python version of the pseudocode step INPUT name
Q3
Pseudocode IF score > 5 THEN becomes an if line ending with a colon → if score > 5:
Q4
Pseudocode FOR i <- 1 TO 5 becomes a for loop with range(1, 6) → for i in range(1, 6):
Q5
score <- 0 → The pseudocode arrow <- was copied into Python. Python needs = instead: score = 0
Q6
if age >= 13 print("Teen") → The if line is missing its colon, and the print line is not indented. END IF is shown by un-indenting, not by a keyword.
Section C: Definitions
Q1
score <- 0 becomes score = 0 — Pseudocode steps map to Python lines: <- becomes =, input becomes input(), if becomes if with a colon, for becomes for in range(). END IF becomes un-indenting.
Section D: Fill in the Blanks
Q1
score = 0
Q2
name = input("Your name? ")
Q3
if score >= 10: print("You win!")
Q4
for i in range(1, 6): print(i)
Q5
if age >= 12 and age <= 15: print("Teen")
Section E: Spot the Error
Q1
B — The pseudocode arrow <- becomes = in Python. The line should be name = input('Name: ').
Q2
A — Python does not use the pseudocode arrow <-. Assignment uses =, so the line should be score = 0.
Q3
A — A Python for loop header must end with a colon. It should be for i in range(5):.
Q4
B — The pseudocode 'greater than or equal to' becomes >= in Python. => is not a valid operator, so it should be if age >= 13:.
Q5
B — A Python for loop needs the keyword in before range(). It should be for n in range(1, 4):.