Computer Science Grade 8 20 min

Testing and Debugging: Finding and Fixing Errors

Students will learn basic debugging techniques to identify and fix errors in their code.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Differentiate between syntax errors and logic errors in Python code. Interpret basic Python error messages to locate a bug's source. Use 'print debugging' to trace the value of variables throughout a program's execution. Create simple test cases with expected inputs and outputs to verify program functionality. Correct common errors such as typos, incorrect indentation, and off-by-one list indexing. Implement a `try...except` block to handle invalid user input gracefully. Ever played a game that suddenly crashed or acted weird? 🐛 Let's learn how to become a 'bug hunter' and fix those problems in our own interactive stories! This lesson will teach you the essential skills of testing and debugging. You'll learn how t...
2

Key Concepts & Vocabulary

TermDefinitionExample BugAn error, flaw, or fault in a computer program that causes it to produce an incorrect or unexpected result, or to behave in unintended ways.In your story, a magic key is supposed to unlock a door, but a bug causes the key to disappear from the player's inventory without the door opening. DebuggingThe process of finding and resolving bugs within a computer program. It's like being a detective for your code.You add `print(player_inventory)` before and after the 'unlock door' code to see when the key disappears and figure out why the door isn't opening. Syntax ErrorAn error in the structure or grammar of the code that violates the rules of the programming language. The program will not run at all with a syntax error.Forgetting the colon in an...
3

Core Syntax & Patterns

The `print()` Debugging Pattern print(f'DEBUG: Variable {variable_name} has value: {variable_name}') Use this pattern to check the value of a variable at a specific point in your code. Place it before and after a line of code that you suspect is causing a problem to see how the value changes. The `try...except` Block for Input try: # Code that might cause an error age = int(input('Enter your age: ')) except ValueError: # Code to run if the error happens print('That is not a valid number!') Use this to prevent your game from crashing when you expect a number but the user types text. The code in the `try` block is attempted, but if a `ValueError` occurs, the `except` block runs instead of crashing. Reading a Traceback Read from the b...

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
A player's final score is calculated with `final_score = rooms_explored * 5`. The player explored 3 rooms, but the game shows a final score of 10. You add `print(f'DEBUG: rooms_explored = {rooms_explored}')` right before the calculation, and it outputs `DEBUG: rooms_explored = 2`. What does this tell you about the bug?
A.There is a syntax error in the score calculation line.
B.The multiplication operator `*` is not working correctly.
C.The bug is a logic error that happened *before* the calculation, causing `rooms_explored` to have the wrong value.
D.The `print` function is changing the value of the variable.
Challenging
A program asks the user to select an item from a list `items = ['sword', 'shield']` by typing 0 or 1. The code is `choice_index = input('Enter index: ')` followed by `chosen_item = items[choice_index]`. This crashes with a `TypeError`. Why does this happen and how should it be fixed?
A.The list is empty; you must add items to it first.
B.The `input()` function returns a string, but list indices must be integers. It should be fixed by converting the input using `int()`.
C.The variable `choice_index` is a `NameError`; it should be `index`.
D.The user entered a number that was too large, causing an `IndexError`.
Challenging
In your game, a variable `scene_index` tracks the player's progress through a list of scenes. The game is supposed to go from index 0 to 1 to 2, but it jumps from 0 directly to 2. The code to advance is `scene_index += 1`. Which of the following is a plausible logic error that could cause this, and what print statement would help confirm it?
A.The line `scene_index += 1` is accidentally written twice in the code. Confirm with `print(f'Advancing scene. New index: {scene_index}')` after each increment.
B.There is a syntax error. Confirm with a `try...except` block.
C.The list of scenes has a duplicate entry. Confirm with `print(len(scenes))`.
D.The computer's processor skipped an instruction. This cannot be debugged with `print`.

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 Interactive Storytelling with Python: Introducing Game Development

Ready to find your learning gaps?

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