Search code examples
pythonerror-reporting

Same mistake but different error report. Why?


Why Python reports about different errors for the same reason, the programs with the 1 and 2 lines of code?

I mean:

print(1

gives

Error: unexpected EOF while parsing

but

a = 1+1
print(1

gives

Error: invalid syntax

same problem - but error message is different - reason?

sys.version_info(major=3, minor=2, micro=0, releaselevel='final',serial=0)


Solution

  • In your first case python is 'looking' for its FIRST line/command. The minimum for a program is at least one statement. So it complains about early termination.

    So the first error EOF (end of File) means: 'Hey I was expecting at least one command and suddenly the line ended...' 'Are you sure the program is completed?'

    The second error the previous was run so the compiler 'knows' that this is a program.

    and the error is different but it means:

    'Hey I for now you have a error in this position, can you fix your sintaxe'?

    I hope my non academic way do not distracted you :)