Logic errors in Python

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

Logic errors in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program runs with no error message but prints the wrong total. What kind of mistake is it?
A A logic error
B A syntax error
2. A game gives a pass mark for scores of 50 or more. Which condition is correct?
A if score >= 50:
B if score > 50:
3. You want to print the numbers 1 to 5. Which loop does this?
A for i in range(1, 6):
B for i in range(1, 5):
4. Which code runs without an error message but is still a logic error?
A if age > 18 without the colon at the end
B if age > 18: when 18-year-olds should be allowed in
5. A program should add 10% tax to a price. Which order of steps is correct?
A price = 50, then print(total), then total = price * 1.1
B price = 50, then total = price * 1.1, then print(total)

Match the Pairs

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

Left columnRight column
1if score > 50: Afor i in range(1, 11):
2for i in range(1, 5): Baverage = total / 3 total = a + b + c
3A logic error: the average is worked out before the total has been added up, so the answer is wrong CThe statements are in the wrong order, so print runs before total is set. Put total = 5 + 10 first, then print(total).
4Fixing an off-by-one error so a loop prints every number from 1 to 10 DA 13 year old is left out because > was used instead of >=. Use age >= 13 to include 13.
5if age > 13: print("Teen") ESkips a score of exactly 50, so a student who needs to score 50 or more to pass would wrongly fail
6print(total) total = 5 + 10 FCounts only 1, 2, 3, 4 and stops before 5, so it is off by one if you wanted 1 to 5

Definitions

logic error

Fill in the Blanks

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

1.
score = 50 if score __ 50: print("Pass")
2.
# Print the numbers 1 to 5 for i in range(1, _): print(i)
3.
total = 0 total = total + 5 print(_____)
4.
score = 100 if score _ 100: print("Bonus!")
5.
# Say hi 3 times for i in range(_): print("Hi")

Spot the Error

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

Question 1
A for i in range(1, 5):
B print(i)
C print('Done: 1 to 5')
Question 2
A age = 13
B print('13 and up only')
C if age > 13:
D print('Welcome')
Question 3
A total = 0
B print('Sum of 1 to 10')
C for i in range(1, 10):
D total = total + i
E print(total)
Question 4
A price = 20
B discount = 5
C pay = price + discount
D print('You pay', pay)
Question 5
A pass_mark = 50
B score = 50
C print('Pass mark is 50 or more')
D if score > pass_mark:
E print('Pass')

Answer Key

Logic errors in Python
Section A: Quick Questions
Q1
A — A logic error
Q2
A — if score >= 50:
Q3
A — for i in range(1, 6):
Q4
B — if age > 18: when 18-year-olds should be allowed in
Q5
B — price = 50, then total = price * 1.1, then print(total)
Section B: Match the Pairs
Q1
if score > 50: → Skips a score of exactly 50, so a student who needs to score 50 or more to pass would wrongly fail
Q2
for i in range(1, 5): → Counts only 1, 2, 3, 4 and stops before 5, so it is off by one if you wanted 1 to 5
Q3
A logic error: the average is worked out before the total has been added up, so the answer is wrong → average = total / 3 total = a + b + c
Q4
Fixing an off-by-one error so a loop prints every number from 1 to 10 → for i in range(1, 11):
Q5
if age > 13: print("Teen") → A 13 year old is left out because > was used instead of >=. Use age >= 13 to include 13.
Q6
print(total) total = 5 + 10 → The statements are in the wrong order, so print runs before total is set. Put total = 5 + 10 first, then print(total).
Section C: Definitions
Q1
logic error — A mistake that lets a program run with no error message but give the wrong result, caused by a wrong condition, operator (> or >=), range() off by one, or steps in the wrong order.
Section D: Fill in the Blanks
Q1
score = 50 if score >= 50: print("Pass")
Q2
# Print the numbers 1 to 5 for i in range(1, 6): print(i)
Q3
total = 0 total = total + 5 print(total)
Q4
score = 100 if score > 100: print("Bonus!")
Q5
# Say hi 3 times for i in range(3): print("Hi")
Section E: Spot the Error
Q1
A — range(1, 5) stops before 5, so it only prints 1 to 4. The program still runs but gives the wrong result. To count 1 to 5 you need range(1, 6).
Q2
C — The rule says 13 and up, so a 13-year-old must be let in. With > a 13-year-old is rejected and no error shows. It should be if age >= 13:
Q3
C — range(1, 10) stops at 9, so 10 is never added and the total is wrong. It should be range(1, 11) to include 10.
Q4
C — A discount should lower the price, but + makes it higher. The program runs with no error but shows the wrong amount. It should be pay = price - discount.
Q5
D — The rule says 50 or more, so a score of exactly 50 should pass. With > a score of 50 fails and no error message shows. It should be if score >= pass_mark: