Showing posts with label Error. Show all posts
Showing posts with label Error. Show all posts

Wednesday, 8 January 2014

Programming error in python

There are Three types of Error in Python Programming .Those are :

1) Syntax Errors
2) Runtime Errors
3) Logic Errors


1) Syntax Errors

The most common erroryou will encounter are syntax errors. Like any programming lan-guage, Python has its own syntax, and you need to write code that obeys the syntax rules. If your program violates the rules—for example, if a quotation mark is missing or a word is misspelled—Python will report syntax errors.

Syntax errorsresult from errors in code construction, such as mistyping a statement, incor-rect indentation, omitting some necessary punctuation, or using an opening parenthesis with-out a corresponding closing parenthesis. These errors are usually easy to detect, because Python tells you where they are and what caused them. For example, the following print statement has a syntax error:




2) Runtime Errors

Runtime errorsare errors that cause a program to terminate abnormally. They occur while aprogram is running if the Python interpreter detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors. Aninput error occurs when the user enters a value that the program cannot handle. For instance, if the program expects to read in a number, but instead the user enters a string of text, this causes data-type errors to occur in the program.

Another common source of runtime errors is division by zero. This happens when the divi-sor is zero for integer divisions. For example, the expression 1 / 0in the following statement would cause a runtime error.



3) Logic Errors :

Logic errorsoccur when a program does not perform the way it was intended to. Errors of this kind occur for many different reasons. For example, suppose you wrote the program in Listing 1.4 to convert a temperature (35 degrees) from Fahrenheit to Celsius.


You will get Celsius –12.55degrees, which is wrong. It should be 1.66. To get the correct result, you need to use 5 / 9 * (35 – 32)rather than 5 / 9 * 35 – 32in the expression. That is, you need to add parentheses around (35 – 32)so Python will calculate that expression first before doing the division.