Online Tutorials & Training Materials | STechies.com
Register Login

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 more code/program lines to be compiled. It can be either the some of syntax is incomplete ie. something is missing in the code or terminated further execution of program.

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

See the following example:-

In this example except is missing which again combination of 'try and except' here in the first code which again incomplete syntax where the control terminates itself as the syntax is incomplete try is there but complete keyword except is missing and therefore SyntaxError: unexpected EOF while parsing error occurred.

Program –1 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

But in the second program which completes the keyword combination with key statements, so the program execution is completed.

Program –2 corrected Program

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)

Cause of this error

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

  • Incomplete parameters may cause this kind of errors.
  • Incomplete functions along with the combination of key statements may incomplete. Need to take care as they called with complete & one closed also. Such kind of words are try and except, if and else, and loop statements.

How to resolve this error?

You need to take care about the parameters and their syntaxes. Need to check all function and their closing statements.

Before called check all parameter of functions are defined. Important thing again the indentation of the program also which automatically help in parameter and functions.

Summary

It is an error raised at the time of parsing which clear that it is a syntax error and also for closing statements & incomplete argument of function. Therefore you need to take care of indentation of the program which helps you to solve this error.


Related Articles