Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

SyntaxError: unexpected EOF while parsing

|| 0

SyntaxError: unexpected EOF while parsing
Stechies

What is Unexpected EOF while parsing Error?

It is a syntax error also known as parsing errors where the control in the program/code reaches to end but there are more code/program lines which are needed to be compiled. It can be either due to some incomplete syntax ie. something missing in the code which terminated further execution of the program.

While executing the program line by line in the console, it is single statement work but not for block of code. 

Cause of this error

SyntaxError: Unexpected EOF while parsing error in Python can occur due to the following reasons.

  • Incomplete parameters may cause this kind of errors.
  • Incomplete functions along with the combination of key statements like 'try and except', if and else, and loop statements may be incomplete.

Therefore we need to take care of these points to avoid this 'SyntaxError: Unexpected EOF while parsing' error

See the following example:-

Program –1 Error 

The Python program below is a combination of 'Try and Except' block. Here in this program, we can see the 'Try' block is present but the 'Except' block is missing and therefore the control terminates itself as the syntax is incomplete. And we get SyntaxError: unexpected EOF while parsing error

def chalu ():
    print ("is where the control ")  
    cont_abc ()
def Cont_abc (): 
    cont_0=raw_input("Command enter ") 
    try: 
        if cont_0 == 'right ':                            
           next_screen () 
        else:  
            print ('right.')  
            cont_abc ()  

Output:

File "Main.py", line 14

SyntaxError: unexpected EOF while parsing

Program –2 corrected Program

Now in this program, the keyword combination with key statements 'Try and Except' is complete, so the program execution is completed.

def chalu ():
    print ("is where the control ")  
    cont_abc ()
def Cont_abc (): 
    cont_0=raw_input("Command enter ") 
    try: 
        if cont_0 == 'right ':                            
           next_screen () 
        else:  
            print ('right.')   
            cont_abc ()
    except Exception as ex:
            print(ex)

Output

...Program finished with exit code 0

How to resolve this Error?

In order to avoid this, please do the following

  • You need to take care of the parameters and their syntaxes. Need to check all function and their closing statements.
  • Before executing the program, please also check if all the parameter of functions is defined.
  • Also, check and correct the indentation of the program

Conclusion

It is an error raised at the time of parsing which clearly implies that it is a syntax error caused due to missing closing statements or incomplete argument of the function. Therefore you need to take care of indentation of the program which helps you to solve this error.


Advertisement
Advertisement
Advertisement