Game Development Reference
In-Depth Information
7 - Using the Debugger
Figure 7-4: The Debug Control window.
The number1 variable has the value 9 and the number2 variable has the value 10 .
When you click Step, the program will display the string in the print() call with these
values. (Of course, we use the str() function so that we can concatenate the string
version of these integers.)
Clicking on Step on line 5 will cause the debugger to wait until the player enters a
response. Go ahead and type in the correct answer (in my case, 19) into the interactive shell
window. The debugger will resume and move down to line 6.
Line 6 is an if statement. The condition is that the value in answer must match the
sum of number1 and number2 . If the condition is True , then the debugger will move to
line 7. If the condition is False , the debugger will move to line 9. Click Step one more
time to find out where it goes.
The debugger is now on line 9! What happened? The condition in the if statement must
have been False . Take a look at the values for number1 , number2 , and answer .
Notice that number1 and number2 are integers, so their sum would have also been an
integer. But answer is a string. That means that the answer == number1 +
number2 condition would have evaluated to '19' == 19 . A string value and an integer
value will always not equal each other, so the condition would have evaluated to False .
That is the bug in the program. The bug is that we use answer when we should be using
int(answer) . Go ahead and change line 6 to use int(answer) == number1 +
number2 instead of answer == number1 + number2 , and run the program again.
What is 2 + 3?
5
Correct!
This time, the program worked correctly. Run it one more time and enter a wrong answer
on purpose to make sure the program doesn't tell us we gave the correct answer. We have
now debugged this program. Remember, the computer will run your programs exactly as
you type them, even if what you type is not what you intend.
Search WWH ::




Custom Search