0

I'm trying to identify a few errors in this code below and write which kind of error it is and replace it with the correct code. I've found the three but I have a question on differentiating whether one is a runtime or a logic error.

def mirror(word):                         #  1
    result == ''                          #  2
    ord_a = ord('a')                      #  3
    ord_z = ord('z')                      #  4
    for c in word[:-1]:                   #  5
        value = ord('c') - ord_a          #  6
        new_value = ord_z - value         #  7
        result += chr(new_value)          #  8
        return result                     #  9

I've successfully identified 4 lines (line 2, line 5, line 6, line 9) and made adjustments (testing various inputs work in the new function) here:

def mirror(word):                         #  1
    result = ''                           #  2 ##### runtime error* or logic?
    ord_a = ord('a')                      #  3
    ord_z = ord('z')                      #  4
    for c in word[::-1]:                  #  5 ##### logic error (supposed to reverse slicing)
        value = ord(c) - ord_a            #  6 ##### logic error (ord(c) not ord('c'))
        new_value = ord_z - value         #  7
        result += chr(new_value)          #  8 
    return result                         #  9 ###### syntax error (wrong indent)

My question is would line 2 count as a runtime error or a logic error, given that in the erroneous code it was comparing equality "==" rather than referencing an assignment with "=" .

Cheers

0

1 Answer 1

4

The errors can be best delineated as follows:

  • A syntax error is something caught by the compiler/interpreter and it's incorrect use of the language itself. For example, for:, which is invalid Python.
  • A runtime error is a problem that cannot be detected before the code runs but causes an issue that is caught during the program run. An example would be x = open("nosuchfile.txt") because the file is checked for existence only at runtime.
  • A logic error is something that isn't caught, either at compile or runtime but which causes an issue. An example would be working out the sum of all numbers from one to n inclusive with the_sum = sum([x for x in range(n)] - that wouldn't include the n in the sum.

If you actually call the function with something like:

print("running")
mirror("hello")

you will see there are no syntax errors because it successfully starts running.

Using the definitions above:

  • result == '' is a runtime error, it detects that you are trying to compare the uninitialised result with something (a result = 7 before that makes the error go away).
  • Everything else is a logic error since it neither prevents compilation nor stops it running because of an unrecoverable fault.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.