Game Development Reference
In-Depth Information
number2))
Type the program in exactly as it is above, even if you can already tell what the bug is.
Then trying running the program by pressing F5. This is a simple arithmetic game that
comes up with two random numbers and asks you to add them. Here's what it might look
like when you run the program:
What is 5 + 1?
6
Nope! The answer is 6
That's not right! This program has a semantic bug in it. Even if the user types in the
correct answer, the program says they are wrong.
You could look at the code and think hard about where it went wrong. That works
sometimes. But you might figure out the cause of the bug quicker if you run the program
under the debugger. At the top of the interactive shell window, click on
Debug
, then
Debugger
(if there is no check already by the Debugger menu item) to display the Debug
Control window. In the Debug Control window, make sure the all four checkboxes (Stack,
Source, Locals, Globals) are checked. This makes the Debug Control window provide the
most information. Then press
F5
in the file editor window to run the program under the
debugger.
The debugger starts at the
import random
line. Nothing special happens here, so just
click
Step
to execute it. You should see the
random
module at the bottom of the Debug
Control window in the Globals area.
Click Step again to run line 2. A new file editor window will pop open. Remember that
the
randint()
function is inside the
random
module. When you stepped into the
function, you stepped into the
random
module because that is where the
randint
function
is. The functions that come with Python's modules almost never have bugs in their code, so
you can just click
Out
to step out of the
randint()
function and back to your program.
After you have stepped out, you can close the
random
module's window.
Line 3 is also a call to the
randint()
function. We don't need to step through this code,
so just click
Over
to step over this function call. The
randint()
function's code is still
executed, it is just executed all at once so that we don't have to step through it.
Line 4 is a
print()
call to show the player the random numbers. But since we are using
the debugger, we know what numbers the program will print even before it prints them! Just
look at the Globals area of the Debug Control window. You can see the
number1
and
number2
variables, and next to them are the integer values stored in those variables. When
I ran the debugger, it looked like this: