Correcting Coding Errors in Python

Correcting Errors

When coding programs there are three common types of error that can occur. It is useful to recognize these different error types in Python programming so they can be corrected more easily:

  • Syntax Error – occurs when the interpreter encounters code that does not conform to the Python language rules. For example, a missing quote mark around a string. The interpreter halts and reports the error without executing the program.
  • Runtime Error  – occurs during execution of the program, at the time when the program runs. For example, when a variable name is later mis-typed so the variable cannot be recognized. The interpreter runs the program but halts at the error and reports the nature of the error as an “Exception”.
  • Semantic Error  – occurs when the program performs unexpectedly. For example, when order precedence has not been specified in an expression. The interpreter runs the program and does not report an error.

Correcting syntax and runtime errors is fairly straightforward, as the interpreter reports where the error occurred or the nature of the error type, but semantic errors require code examination.

 

Programming errors are often called “bugs” and the process of tracking them down is often called “debugging”.

 

 

 

Step 1
Open an IDLE Edit Window then add a statement to output a string that omits a closing quote mark
print( ‘Coding for Beginners in easy steps )

 

Step 2
Save then run the program to see the interpreter highlight the syntax error and indicate its nature

Beware-New

The red syntax error indicator points to the line where the End Of Line (EOL) error occurs.

Step 3
Insert a quote mark before the closing parenthesis to terminate the string and save then run the program again – to see the error has been corrected

Step 4
Next, begin a new program by initializing a variable then try to output its value with an incorrect variable name – to see the interpreter report a runtime error
title = ‘Coding for Beginners in easy steps’
print( titel )

 

 

 

 

 

 

 

Step 5
Amend the variable name to match that in the variable declaration and save then run the program again – to see the error has been corrected

Step 6
Now, begin a new program by initializing a variable then try to output an expression using its value without explicit precedence – to see a possibly unexpected result of 28
num = 3
print( ‘Result: ‘ , num * 8 + 4 )

Step 7
Add parentheses to group the expression as 3 * ( 8 + 4 ) then save the file and run the program again – to see the expected result of 36, correcting the semantic error

… and it’s as easy as that!

 

 

Want to know more?

For the complete Coding for Beginners guide, in the trusted In Easy Steps style, click here.

Coding for Beginners in easy steps has an easy-to-follow style that will appeal to anyone, of any age, who wants to begin coding computer programs. You need have no previous knowledge of any computer programming language so it’s ideal for the newcomer, including youngsters needing to learn programming basics for the school curriculum.

See also: Exploring IDLE in Python