Log in / Register
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

1.8 Writing Your Second Program

Writing programs is an error-prone activity. Programmer's almost never write a non-trivial program perfectly the first time. As programmers we need a tool like an Integrated Development Environment (i.e. IDE) that helps us find and fix our mistakes. Going to the File menu of the Wing IDE window and selecting New opens a new edit pane. An edit pane can be used to write a program but it won't execute each line as you press enter. When writing a program we can write a little bit and then execute it in the Python interpreter by pressing F5 on the keyboard or by clicking the debug button.

When we write a program we will almost certainly have to debug it. Debugging is the word we use when we have to find errors in our program. Errors are very common and typically you will find a lot of them before the program works perfectly. Debugging refers to removing bugs from a program. Bugs are another name for errors. The use of the words bug and debugging in Computer Science dates back to at least 1952 and probably much earlier. Wikipedia has an interesting discussion of

Fig. 1.13 The Wing IDE

the word debugging if you want to know more. While you can use the Python Shell for some limited debugging, a debugger is a program that assists you in debugging your program. Figure 1.13 has a picture of the Wing IDE with the program we've been working on typed into the editor part of the IDE. To use the debugger we can click the mouse in the area where the red circle appears next to the numbers. This is called setting a breakpoint. A breakpoint tells Python to stop running when Python reaches that statement in the program. The program is not finished when it reaches that step, but it stops so you can inspect the state of the program.

The state of the program is contained in the bottom left corner of the IDE. This shows you the Stack Data which is just another name for the program's state. You can see that the variables that were defined in the program are all located here along with their values at the present time.

Practice 1.9 Create an edit pane within theWing IDE and write the programas it appears in Fig. 1.13. Write a few lines, then run it by pressing F5 on thekeyboard or clicking on the Debug button. The first time you press F5 you willbe prompted to save the program. Make sure you save your program whereyou can find it later.

Try setting a break point by clicking where the circle appears next to thenumbers in Fig. 1.13. You should see a red circle appear if you did it right.Then run the program again to see that it stops at the breakpoint as it appearsin Fig. 1.13. You can stop a program at any point by setting a breakpoint onthat line. When the debugger stops at a breakpoint it stops before the statementis executed. You must click the Debug button, not the Run button to get it tostop at breakpoints.

Look at the Stack Data to inspect the state of the program just before the wordDone is printed. Make sure it matches what you see here. Then continue theexecution by clicking the Debug button or pressing F5 again to see that Doneis printed.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science