Skip to content

Latest commit

 

History

History
112 lines (83 loc) · 4.97 KB

File metadata and controls

112 lines (83 loc) · 4.97 KB

Debugging

The debugging process in software development begins when a developer locates and reproduces a coding error in a computer program.So, we'll be learning about how you should operate as a developer. Style recommendations for your codes, problem-solving tactics, habits, and attitudes that will allow you to enjoy programming even when it's difficult and stressful.

We'll read about three types of errors; Syntax errors, Runtime errors and Semantic errors.

Syntax Error

Syntax refers to the structure of a program and the rules about that structure. For example,

in English, a sentence must begin with a capital letter and end with a period. this sentence contains a syntax error. So does this one

Syntax rules in Python include requirements such as these: Statements must generally be written one per line; the print statement must surround the value to be shown in parenthesis; expressions must be correctly formed. The following lines contain syntax errors:

print(Hello, world!)
print "Hello, world!"
print(5 + )

above we have SyntaxError.

print("Hello, world!")
print ("Hello, world!")
print(5 + 5 )

Now this is okay.

RunTimeError

RunTimeError is another error. A runtime error occurs when a program passes the interpreter's syntax tests but fails to execute. However, an error occurred during the execution of one of the program's statements, causing the interpreter to stop running the program and display an error message. Runtime errors are often known as exceptions since they usually indicate that something unusual has happened. Some common runtime errors:

1)Variable and function names that are misspelled or capitalized incorrectly 2)Attempts to execute operations (such as math operations) on incorrectly typed data (ex. attempting to subtract two variables that hold string values) 3)Dividing by zero 4)Attempts to use a type conversion function such as int on a value that can’t be converted to an int

subtotal = input("Enter total before tax:")
tax = .08 * subTotal
print("tax on", subtotal, "is:", tax)

the above program contains various runtime errors.

subtotal = int(input("Enter total before tax:"))
tax = .08 * subtotal
print("tax on", subtotal, "is:", tax)

Now this is okay.

1)If the error message mentions SyntaxError, you know that the problem has to do with syntax: the structure of the code, the punctuation, etc.
2)If the program runs partway and then crashes, you know the problem is a runtime error. Programs with syntax errors don’t execute even one line.

Semantic error

The semantic error, often known as a logic error, is the third form of error. If your program contains a semantic error, it will execute successfully in the sense that no error messages will be generated by the machine. Your program, on the other hand, will not do the proper thing. It is going to do something else.Specifically, it will do what you told it to do, not what you wanted it to do.

x = input('Enter a number:')
y = input('Enter another number:')
sum = x + y

print('The sum of', x, 'and', y, 'is', sum)

It contains a semantic error. The error is that the program performs concatenation instead of addition, because the programmer failed to write the code necessary to convert the inputs to integers.

x = int(input('Enter a number:'))
y = int(input('Enter another number:'))
sum = x + y

print('The sum of', x, 'and', y, 'is', sum)

Now this is okay.

It can be difficult to spot semantic problems because no error message appears to indicate that the results are incorrect. Only if you know ahead of time what the program should do with a specific set of input can you find semantic mistakes. Then you run the program with that data as input and compare the results to what you expected. You can conclude that there is either 1) a semantic error or 2) an error in your expected results if the actual output differs from the expected output.

To detect a semantic error in your program, you need the help of something called a test case.

A test case is a set of input values for the program, together with the output that you expect the program should produce when it is run with those particular inputs.

Here is an example of a test case for the program above:

Test Case
---------
Input: 2, 3
Expected Output: 5

If you give someone this test case and ask them to run the program, they can type in the inputs, watch the output, compare it to the expected output, and determine whether or not there is a semantic problem based on whether the actual output matches the intended output. The tester doesn’t even have to know what the program is supposed to do. In this case, the program is so simple that we don’t need to write down a test case at all; we can compute the expected output in our heads with very little effort. More complicated programs require effort to create the test case. So, creating test cases is an important part of the work that programmers perform in order to help them produce programs that work correctly.