A SyntaxError is raised by the parser when it detects that your syntax is incorrect, for example, missing colons, brackets, incorrect statements, etc. 'I will not allow you to execute your code until you fix this problem.
Your code will only cause a run-time error, that is, when the tofloat(i) function is called for the first time, so this is a run-time error. In particular, NameError .
Also, a runtime error will not stop the execution of your program until this error is executed. This way your code may work fine if you don't call tofloat ever.
The code below executes correctly to the third line, but then stops when raising a NameError ( NameError error)
print 1 print 2 print 3 print foo
output:
1 2 3 Traceback (most recent call last): File "so.py", line 4, in <module> print foo NameError: name 'foo' is not defined
This code will not be executed since we made a SyntaxError , although the first 3 lines are fine:
print 1 print 2 print 2 print (foo
Output:
$ python so.py File "so.py", line 5 ^ SyntaxError: invalid syntax
Please note that there is also a python RunTimeError that occurs when an error is detected that does not fall into any of the other categories
source share