Syntax, Runtime and Logical Errors

Syntax, Runtime and Logical Errors Tutorial Video

Can’t access YouTube? Click Here

Syntax Errors

Syntax, Runtime and Logical Errors

These are errors where the computer cannot interpret the inputted code and therefore cannot compile or translate the code.

These ares are usually the first errors that a programmer will encounter and are the easiest to spot, as the computer will usually inform you of the offending line.

Example 1

name = input('Nice to meet you)

Here the code is missing a closing inverted comma.

Example 2

age = int(input("how old are you?")))

Here the code has closed too many brackets.

Runtime errors

These are errors that are not detected until the program is running. The program executes successfully until a certain point, where it crashes.

These errors often occur because of:

  • Failing to does type conversions (e.g. from string it integer)

Example 1

age = input('How old are you?')

if age > 50:

    print('You are old!')

Here the program will successfully run and ask for an age input. However on the second line the program will crash because age has been input as a string and therefore needs to be reassigned or cast as an integer before being compared to the 50.

Example 2

shopping = ['cheese','egg','beef']

print(shopping[3])

Here the program will crash when it attempts to  print out index 3. This is because Python starts indexing from 0, not 1 and therefore item 3 does not exist.

 

Logical Errors

These are errors that do not cause the program to crash, but simply do not function as the code intended. Since the program does not crash, these errors are often the most difficult to spot and therefore the most dangerous errors. These errors can only be detected through thorough testing.

Example 1 

age = int(input('How old are you?'))

if age < 50:

    print('You are old!')

else:

    print('You are young!')

Here the code will execute without an error, however the comparison operator < is the wrong one. It’s should be <.

Example 2

while True:

    answer = input('Press enter to break out of the loop')

    if answer == "   ":

        break

Here the program contains too many spaces inside of the ”    “. This will mean that the user would have to input 3 spaces for the program to break, not one. The program would not crash, it would simply keep looping forever…