← VCE Software Development U3O1

Logic errors in Python

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.

Understanding Logic errors in Python in VCE Software Development U3O1

What it does: The steps are in the wrong order. The program runs but total is used before it is calculated (here it would only work if total already held an old value). range() stops before the end number, so the end must be 11 to include 10. A score of exactly 50 should pass. With > 50, a score of 50 fails, which is a logic error. Use >= to include 50. A missing colon gives a syntax error and an error message. Using > instead of >= runs fine but leaves out 18-year-olds, so it is a logic error. Python can't know what you meant, so it runs your code as written. Test with different inputs and check the results to find logic errors.

Common mistakes: 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). 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:.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 156–166) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A program runs with no error message but prints the wrong total. What kind of mistake is it?
A syntax errorA logic error
Matches
if age > 13: print("Teen")
↓
A 13 year old is left out because > was used instead of >=. Use age >= 13 to include 13.
Applying
Meanings
Term
logic error
Meaning
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.
Practice
total = 0
Testing
Gaps
if score 50:
Check Answer
Spots
1for i in range(1, 5):
2 print(i)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Seahorse' badge — Range Rider, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
A logic error lets the program run without a message but gives the wrong result. A syntax error stops the program before it starts.
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:
A score of exactly 50 should pass. With > 50, a score of 50 fails, which is a logic error. Use >= to include 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):
range() stops just before the end number. range(1, 5) only gives 1 to 4, so it is off by one. Use range(1, 6) to include 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 ✓
A missing colon gives a syntax error and an error message. Using > instead of >= runs fine but leaves out 18-year-olds, so it is a logic error.
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) ✓
The steps are in the wrong order in the second one. The program prints total before it works it out, which causes an error or the wrong output. Calculate first, then print.

Match the Pairs

if score > 50: → Skips a score of exactly 50, so a student who needs to score 50 or more to pass would wrongly fail
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
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
Fixing an off-by-one error so a loop prints every number from 1 to 10 → for i in range(1, 11):
if age > 13: print("Teen") → A 13 year old is left out because > was used instead of >=. Use age >= 13 to include 13.
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).

Key Term

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.

Fill in the Blanks

1. score = 50 if score >= 50: print("Pass")
Hint: Type the comparison operator that lets a score of exactly 50 pass too
2. # Print the numbers 1 to 5 for i in range(1, 6): print(i)
Hint: Type the stop number for range(), which is one more than the last number to print
3. total = 0 total = total + 5 print(total)
Hint: Type the variable name to print after it has been updated, so the new value shows
4. score = 100 if score > 100: print("Bonus!")
Hint: Type the operator that gives a bonus only for scores over 100, not exactly 100
5. # Say hi 3 times for i in range(3): print("Hi")
Hint: Type the number that makes the loop repeat exactly the right number of times

Spot the Error

Question 1
A for i in range(1, 5):
B print(i)
C print('Done: 1 to 5')
Line 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).
Question 2
A age = 13
B print('13 and up only')
C if age > 13:
D print('Welcome')
Line 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:
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)
Line 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.
Question 4
A price = 20
B discount = 5
C pay = price + discount
D print('You pay', pay)
Line 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.
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')
Line 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:

Student with a class code? Join your class →