Computer Science Grade 5 20 min

Fixing Common Errors

Fixing Common Errors

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Identify the difference between a syntax error and a logic error. Trace a loop with a variable to find an infinite loop error. Correct an 'off-by-one' error in a loop. Fix a complex conditional statement that uses the wrong comparison operator. Use 'print' statements as a simple debugging tool to check variable values. Explain how to methodically test code to find a bug. Have you ever built something awesome with blocks, only for it to fall down because one piece was in the wrong spot? 🐛 Let's learn how to find those wobbly blocks in our code! In this lesson, we will become code detectives! We'll learn about common mistakes that happen in more advanced programs, like loops that never end and conditions that don't work...
2

Key Concepts & Vocabulary

TermDefinitionExample BugAn error or mistake in a computer program that causes it to produce an incorrect or unexpected result.In a game, you collect 10 coins but the score only shows 9. The mistake in the code that caused this is a bug. DebuggingThe process of finding and fixing bugs in a program. It's like being a detective for your code.Adding a print command to see the value of your 'score' variable to figure out why it's not counting correctly. Logic ErrorA bug where the code runs without crashing, but it doesn't do what you intended it to do. The computer follows your instructions perfectly, but the instructions are wrong.You want a spaceship to move up when you press the up arrow, but you accidentally coded it to move down. The program runs, but the logic i...
3

Core Syntax & Patterns

The Print Check print(variable_name) When you're not sure what value a variable holds at a certain point in your program, add a print statement. This will display the variable's value in the output, helping you see exactly what the computer is thinking. The Manual Trace Read code line-by-line, pretending you are the computer. Use a piece of paper to keep track of how variable values change. This is a powerful way to find logic errors. By manually tracing the flow of your program, especially through loops and conditionals, you can often spot where your logic went wrong before the computer does. Check Your Condition Is it `>` or `>=`? Is it `<` or `<=`? Is it `==` or `=`? Always double-check the comparison operators in your `if` statements and `wh...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
This code is supposed to print the numbers 1, 2, 3, and 4. Instead, it prints 0, 1, 2, 3, and 4. Which single change will fix the bug? `num = 0` `while (num <= 4):` ` print(num)` ` num = num + 1`
A.Change `num = 0` to `num = 1`.
B.Change `while (num <= 4)` to `while (num < 4)`.
C.Change `print(num)` to `print(num + 1)`.
D.Remove the line `num = num + 1`.
Challenging
A prize is awarded if `score` is over 50 AND `level` is 3 or more. The code below is not working correctly for a player with a score of 60 and level of 3. What is the logical flaw? `if (score > 50 or level > 3):` ` print("You win!")`
A.The `or` should be an `and`.
B.The condition `level > 3` should be `level >= 3`.
C.Both the `or` should be `and`, and `level > 3` should be `level >= 3`.
D.The `>` operators should be `<` operators.
Challenging
A student wants their program to count down from 3 to 1 ("3, 2, 1"). Predict the behavior of their code. `count = 3` `while (count > 0):` ` print(count)` ` count = count + 1`
A.It will print "3, 2, 1".
B.It will print "3" and then stop.
C.It will cause a syntax error.
D.It will create an infinite loop, printing 3, 4, 5, ...

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.