Syntax, runtime and logical errors

Syntax errors

Are errors in the code that the computer cannot interpret, and then the program stops.

In Python these errors are often:

  • Spelling errors
  • The omission of important characters (such is a missing colon)
  • Inconsistent use of ‘/’ or wrong indentitation

 

Examples

In the first one is missing the parenthesis, printing EOF error or End Of File error

Syntax error 4
Missing parenthesis at the end of line 1. Look at the circumflex accent ‘^’, showing the location of the error in the file

 

Syntax error 5
CORRECT!

 

In the second one there are two errors, missing colon and no indentation

Syntax error 1
Missing colon

 

Syntax error 2
Missing indentation

 

Syntax error 3
CORRECT!

 

Runtime errors

These are errors that are not detected until runtime.

These are often caused by:

  • It can’t find some date because it doesn’t exist
  • It can perform an action on the data it has been given because it is an invalid type of data

Typical runtime errors are:

  • Division by zero
  • Performing an operation on incompatible types
  • Using an identifier which has not been defined
  • Accessing a list element, dictionary value or object attribute which doesn’t exist
  • Trying to access a file which doesn’t exist

This type of errors are only found at runtime and often through lots of testing.

 

Examples

  • Division by zero: Impossible!

Syntax error 6

  • Data type error: Input a string that must be converted to integer

Sybtax error 7

Syntax error 8
CORRECT!
  • Indexing error: Python starts indexing lists from 0. The ‘sugar’ item is the number [2]

Syntax error 9

Syntax error 10

There was a runtime error in converting between a floating point number and an integer, that caused a $370 million lost in 45 second (video) [ + ]

Ariane 5 crash

 

Logical or semantical errors

These are errors in the code that do not throw an error at all, but simply do not do what you intended the code to do. The program doesn’t crash. These are the most difficult to spot, because they can only be found trough full and extensive testing.

 

Examples

  • Operator in the wrong direction and must be switched to have a correct logic output

Syntax error 11

Syntax error 12

In September 1999, after almost 10 months of travel to Mars, the Mars Climate Orbiter burned and broke into pieces. On a day when NASA engineers were expecting to celebrate, the ground reality turned out to be completely different, all because someone failed to use the right units, i.e., the metric units! The Scientific American Space Lab made a brief but interesting video on this very topic [ + ].

Previous
Quiz - 005
Cursos
Python I - Unit 02
Python
Expressions and Variables
Data types - Introduction 1 points
Printing different values 1 points
Quiz - 005 10m
Quiz - 006 20m