Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python_A4

The document outlines a coding exercise called 'Catch the Bugs' where participants learn to debug Python code by intentionally introducing errors and then fixing them. It provides step-by-step instructions on how to manipulate the code, observe error messages, and understand the debugging process. The session culminates in a challenge to create and fix various bugs in the program.

Uploaded by

biji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python_A4

The document outlines a coding exercise called 'Catch the Bugs' where participants learn to debug Python code by intentionally introducing errors and then fixing them. It provides step-by-step instructions on how to manipulate the code, observe error messages, and understand the debugging process. The session culminates in a challenge to create and fix various bugs in the program.

Uploaded by

biji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Session 1

Catch the Bugs


You are on the final step of the Coding Jungle mission. To complete it, you must catch bugs by
swinging a net left or right. If you miss one, the game is over.

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

1.  Open IDLE (Python).

 From the File menu, select Open.


Select catch_bugs. (Ask your teacher where this is located.)

 From the File menu, select Save As. Go to the place where you save your work.

 Add student name to the file name. Click Save.

Play the Catch the Bugs Game

2.  From the Run menu, select Run Module.

 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.

Copyright © TechnoKids Inc. 12 TechnoPython | Python


Session 1

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!

3.  Delete the second line of code. It imports the Random library:

import random

 From the File menu, select Save.

 From the Run menu, select Run Module.


An error message will show in the Python Shell:
CATCH THE BUGS
Look for clues such as not
There are many bugs in the Coding Jungle.
How many can you catch? defined. This can mean a
Traceback (most recent call last): library was not imported.
File "C:\Users\Student\Documents\name catch_bugs.py", line 14,
in <module>
place=random.randint(1, 2)
NameError: name 'random' is not defined
>>>

 Read the error message to find clues as to what is wrong.

 Close the Python Shell.

 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

bug_list('Name Error Bee', 'Invalid Syntax

 From the File menu, select Save.

 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.

bug_list=('Name Error Bee', 'Invalid Syntax

Copyright © TechnoKids Inc. 13 TechnoPython | Python


Session 1

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!

5.  Delete the quotes around the code print('CATCH THE BUGS')

print(CATCH THE BUGS)

 From the File menu, select Save.

 From the Run menu, select Run Module.


A syntax error message appears. The letter T is highlighted to show the problem.

The highlight does not identify


the exact place of the error.
You need to look before it.
print(CATCH THE BUGS)

 Click OK.

 Add the quotes around the text. Use your skills to run the program.

print('CATCH THE BUGS')

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 ')

direction=input('1 – LEFT, 2 – RIGHT '

 Use your skills to run the program.


A syntax error message appears. The letter d is highlighted.

direction=input('1 – LEFT, 2 – RIGHT '


direction_num=int(direction)

Look at the line above the


highlight to find what is missing.

 Click OK.

 Add the bracket to fix the code. Use your skills to run the program.

direction=input('1 – LEFT, 2 – RIGHT ')

Copyright © TechnoKids Inc. 14 TechnoPython | Python


Session 1

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

Notice that the comment is no longer red when you


remove the #. The color of the code can give you a
hint that something is wrong.

 From the File menu, select Save.

 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.

Remove an Indent Then Read the Message in the Dialog Box

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)

 Use your skills to run the program.


A syntax error message appears. It states that Python expected an indented block.

Colons : tell Python the following


lines of code are grouped
together. The code must be
indented to form a block. while catch_bugs:
place=random.randint(1, 2)

 Click OK.

 Add four spaces to indent the line correctly. Use your skills to run the program.

Take the Debugging Challenge


9. Create bugs, read the error message, and then fix the code:
 Spell a function wrong. Change print to prin()
 Delete a comma from the code print('GAME OVER! Bugs Caught: ', score)
 Delete a colon. Remove it from if direction_num == place:

Close the Program

Copyright © TechnoKids Inc. 15 TechnoPython | Python

You might also like