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?

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

While debugging the program line by line in the console, you will notice that the error occurred because of some wrong single statement not due to 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 incomplete loop statements.

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

See the following example:-

Example with Error:

dictionary={

'name':'ram',

print(dictionary['name'].upper()

Output:

SyntaxError: unexpected EOF while parsing

In the above code example we have created a dictionary, and did not close the '}'bracket, So when compiler was compiling the code it could'nt find the '}' bracket and trow the error code “unexpected EOF while parsing” as output.

Example with solution:

dictionary={

'name':'ram',

}

print(dictionary['name'].upper())

Output:

Output: RAM

Program –1 Error 

The Python program below is a combination of 'Try and Except' block. In this program, we can see the 'Try' block is present but the 'Except' block is missing and therefore the control terminates itself due to incomplete syntax. And you 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 Solved 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 check if all the parameter of functions is defined.
  • Also, check and correct the indentation of the program

Conclusion

It is an error occured 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