You can learn about Introduction to Programming Using Python Programs with Outputs helped you to understand the language better.

Introduction to Python Programming – Testing and Debugging

Testing and Debugging

Even after taking full care of the program design, some errors may remain in the program as the program designer might never have thought about a particular case. These errors are detected only when you start executing the program on a computer. Such types of program errors are called bugs, and the pro¬cess of removing these errors is known as debugging. A flowchart is very helpful in detecting, locating, and removing mistakes (bugs) in a program in a systematic manner.

  • Program debugging is the process of finding and correcting errors ( “bugs”) in a computer program.

Every program, written in any language, after locating errors, if any and removing them, must be executed to see the output.
Testing and debugging are vital steps in developing computer programs. They are also time-consuming steps. In fact, the time spent in testing and debugging often equals or exceeds the time spent in program writing. There are three types of errors that occur in a computer program. These are:

a. Syntax Errors
b. Run-time Errors
c. Logical Errors

Syntax Errors

Syntax errors occur in the programs when the rules or syntax of the programming language is not followed. Such programming errors typically involve incorrect punctuation, incorrect word sequence, undefined terms, or misuse of terms. The Python interpreter immediately reports it, usually along with the reason. For example, you get this error message when you try to use an equals sign (=) to do an arithmetic calculation:
>>> 5 – 6 =
SyntaxError: invalid syntax
>>> print “hello”
SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(“hello”)? In Python 3.x, print is a built-in function and requires parentheses. The statement above violates this usage and hence syntax error is displayed. All syntax errors must be found and corrected before a program is made to run. Almost all language pro¬cessors are designed to detect syntax errors. The error messages are useful as they help the programmers rectify all syntax errors in their pro¬grams. Thus, it is a relatively easy task to detect and correct syntax errors.

  • A syntax error can also occur in a calculator when an invalid equation is entered into it. This can be caused either by opening brackets without closing them or by entering several decimal points in one number.

Runtime Errors

Runtime errors occur when a program is executed on the computer, but the desired results are not achieved due to some misinterpretation of a particular instruction. This could be something like dividing a number by zero which results in a very large value of quotient. It may also be any other instruction that the computer is not able to understand. For example, during run time if you try to open a file that does not exist in the hard disk, then it will create a runtime error. To overcome this problem, there is a built-in error detector in the language interpreter or compiler which will give the message that reflects the reason for the runtime error. Most runtime errors are easy to identify because the program halts when it encounters them. For example, an infinite loop or wrong input value says, dividing a number by 0 will produce a runtime error.
>>> a = 1
>>> b = 0
>>> print(a/b)
Trackback (most recent call last):
File “<pyshell#l>”, line l,in <module>
ZeroDivisionError: division by zero

  • ZeroDivisionError is thrown when the second operator in the division is zero.

A runtime error is also called an exception. A number of built-in exceptions are defined in the Python library. For example, TypeError is thrown when an operation or function is applied to an object of an inappropriate type.

>>> ‘ 2 ‘ +2
Traceback (most recent call last):
File “<pyshell#5>”, line 1, in <module>
‘ 2’ +2
TypeError: unsupported operand type(s)
for +: ‘int’ and ‘str’

  • A runtime error is an error that causes abnormal termination of a program during the execution of a code.

Logical Errors

Logical errors are the errors that occur in planning the program logic. In this case, the language processor successfully translates the source code into machine code. The computer actually does not know that an error has been made. It follows the program instruc¬tions and outputs the results, but the result of the output may not be correct.

The problem is that the logic being followed in the solution of the problem does not produce the desired results. When a logical error occurs, the computer does not print the correct output. The computer does not tell you what is wrong. For example, if instruction should be:
a = b * c
but has been written as:
a = b + c

This error will not be detected by the language processor since no language rules have been broken. However, the output will produce the wrong result. Thus, it is an example of a logical error. The logical error in program source code results in incorrect or unexpected results. It is a type of runtime error that may simply produce the wrong output or may cause a program to crash while running.
In the following program, the while loop will not execute even a single time since the initial value of i is 100. i = 100
while i<10:
i= i+2
print ( i )

  • A logical error is a mistake in a program source code that results in incorrect or unexpected behavior.

Semantic error is the error that occurs when a particular statement is unable to provide meaning. For example, the following statement is semantically incorrect since an expression cannot come on the left side of an assignment statement,
a + b = c
This can be written as follows:
c = a + b
Now, this is both semantically and syntactically correct.

  • Semantic error is an error in a program that makes it do something other than what the programmer intended. Syntax errors are caused by invalid syn¬tax. Semantic (logical) errors are caused by errors in program logic.

How to locate Logical Errors?

In order to determine whether or not the logical error exists, the program must be tested. The pur¬pose of testing is to determine whether the results are correct or not. The testing procedure involves running the program to process input test data that will produce known results. By running the pro¬gram and comparing the results, the accuracy of the program logic can be determined.
Logical errors occur typically due to either incorrect logic or missing logic. If the logic is wrong, the answers generated from the test data will be wrong. Errors caused by the missing logic result from logical situations that the program was not designed to handle.

As an example, suppose that a numeric field (like a number 1234) is to be used in an arithmetic process, and the data entry operator enters a value that is not numeric for the field (such as 1-2-3-4). The program logic should determine that the data is not numeric prior to attempting the arithmetic process. If that logic is missing and non-numeric data is used in the arithmetic operation, the program will fail. This error occurred only because the non-numeric data was entered into a numeric field.

Leave a Reply

Your email address will not be published. Required fields are marked *