Python_A4
Python_A4
After you play the game you will add bugs or mistakes into a program. You will then read the
error messages and fix the code. This will help you learn how to debug Python. Finding and fixing
mistakes is called debugging.
Please note – you need the catch_bugs template to complete the task.
Open the Catch the Bugs Program in IDLE and Rename the File
From the File menu, select Save As. Go to the place where you save your work.
The Python Shell opens. It reads the code and then shows the output. Play the game:
Pick a direction:
1 – LEFT
2 – RIGHT
Press ENTER.
Play the game until you miss a bug. How many did you catch?
Click Close X to exit the Python Shell and view the IDLE Editor Window.
Make a Bug Then Read the Error Message to Look for Clues
The game uses the Random Library to set the place of the bug. The program must import the
library to be able to use the commands or functions. The game will not work if there is no library
because some of the words are not defined. Delete import random to see what happens!
import random
From the Edit menu, select Undo to add the code: import random
Break the Code Then Find the Line Number with the Bug in the Error Message
The program has a list of bug names. These are used to randomly pick a bug for the player to
catch. A variable requires an equal sign between the name and the items. If it is not in the code,
the program will show an error. Delete = to break the code.
4. Delete the equal sign from the code bug_list=('Name Error Bee', 'Invalid Syntax
From the Run menu, select Run Module. Look for a line number
An error message will show in the Python Shell: in the error message.
Traceback (most recent call last):
File "C:\Users\Student\Documents\name catch_bugs.py", line 5,
in <module>
bug_list('Name Error Bee', 'Invalid Syntax Bug', 'Indented
Block Beetle', 'Syntax Error Bug', 'Not Defined Fly')
NameError: name 'bug_list' is not defined
>>>
Right click on the line number in the error message. Select Go to file/line.
Add the equal sign back into the code at line 5. Use your skills to run the program.
Delete Quotes Then Look Before the Highlighted Text to Find the Error in the Same Line
The game provides player instructions using the print function. The text must go inside a
bracket and quotes. The program will have a syntax error if punctuation is missing. Try it!
Click OK.
Add the quotes around the text. Use your skills to run the program.
Delete a Bracket Then Look in the Line Above the Highlighted Text to Pinpoint the Error
The player picks the direction the net will move to catch the bug. If the line of code is missing a
bracket an error will occur. The program will highlight text to help the programmer find the
problem. Let's test that out!
6. Delete the end bracket ) from the line direction=input('1 – LEFT, 2 – RIGHT ')
Click OK.
Add the bracket to fix the code. Use your skills to run the program.
Bust the Code Then Use Color Coding as a Hint that Something is Wrong
The game has comments. These are notes about parts of the program. Comments are red.
Color coding can be used to find mistakes. Remove the # from a comment. What happens?
7. Delete the # from the line #player swings net left or right
player swings net left or right
From the Run menu, select Run Module. Another syntax error message appears.
Click OK.
Add # to fix the code. Use your skills to run the program.
The game keeps playing if the player catches a bug. It ends when the player misses a bug. This
part of the program uses a loop. Instructions that repeat must be stacked together. This is done
by indenting each line the same number of spaces. This creates a block of instructions that run
as a group. If the indent is wrong an error will happen. Try it!
8. Change the indent of the first instruction in the loop while catch_bugs:
PRESS BACKSPACE
while catch_bugs:
place=random.randint(1, 2)
bug_name=random.choice(bug_list)
Click OK.
Add four spaces to indent the line correctly. Use your skills to run the program.