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

1.17 When Things Go Wrong

As a programmer, you will soon discover that things can go wrong when writing a program. No programmer writes every program correctly the first time. We are all human and make mistakes. What makes a programmer a really good programmer is when they can find their mistakes and correct them. Debugging programs is a skill that can be learned and therefore can be taught as well. But, it takes lots of practice and patience. Fortunately, you will have many chances to practice as you work your way through this book.

Sometimes, especially when you are first learning to debug your programs, it can help to have someone to talk to. Just the act of reading your code to someone else may cause you to find your mistake. Of course, if you are using this text as part of a course you may not want to read your code to another class member as that may violate the guidelines your instructor has set forth. But, nevertheless, you might find that reading your code to someone else may help you discover problems. This is called a code walk-through by programming professionals. It is a common practice and is frequently required when writing commercially available programs.

There is no substitute for thorough testing. You should run your program using varied values for input. Try to think of values that might cause your program to break. For instance, what if 0 is entered for an integer? What if a non-integer value is entered when an integer was required? What happens if the user enters a string of characters when a number was required?

Sometimes the problems in our code are not due to user input. They are just plain old mistakes in programming caused either by temporarily forgetting something, or by our misunderstanding how something works. For instance, in this chapter we learned about assignment statements. You can store a value in the memory of a computer and point a named reference at the value so you can retrieve it later. But, you must assign a name to a value before you can retrieve it. If you don't understand that concept, or if you forgot where you assigned a value a name in your program, you might accidentally write some code that tries to use that value before it is assigned a name. For instance, consider the program in Fig. 1.21. The program is trying to use the gallons variable which has not been assigned a value. The error message is on the right side of the window. The line where the error was first detected by Python is highlighted.

In the example in Fig. 1.21 the actual error is not on the line that is highlighted. The highlighted line is the line where Python first detected the error. This is a very common occurrence when debugging. Detection of an error frequently occurs after the location of the actual error. To become a good programmer you must learn to look backwards through your code from the point where an error is detected to find the location where it occurred. In this case, the gallon variable should have been written as gallons on line 3 but was incorrectly typed.

Another common error is the index out of range error. This can occur when trying to access a value in a sequence by indexing into the sequence. If the index is for an item that is outside the range of the sequence, an index out of range error will occur. For instance, if you have a string called x that is one character long and you try to access the second element of the string, your program will abort with an index out of range error. Figure 1.22 shows this happening in a snippet of code.

Once again, in the example in Fig. 1.22 the error did not occur on the line that is highlighted. The error occurred because the programmer meant to take the str(83) which would result in “83” as a string instead of the chr(83) which results in the string “S”. If the string had been “83” then line 3 would have worked and would have printed 3 to the screen.

When an error occurs it is called an uncaught exception. Uncaught exceptions result in the program terminating. They cannot be recovered from. Because uncaught

Fig. 1.21 A run-time error

Fig. 1.22 An index out of range error

exceptions result in the program terminating it is vital to test your code so that all variations of running the program are tested before the program is released to users. Even so, there are times when a user may encounter an error. Perhaps it has happened to you? In any case, thorough testing is critical to your success as a programmer and learning to debug and test your code is as important as learning to program in the first place. As new topics are introduced in this text, debugging techniques will also be introduced to provide you with the information you need to become a better debugger.

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel