Table of Contents
Aim:
In this article, we will discuss how to fix SyntaxError: unexpected EOF while parsing in Python?
This article dissects our problem with examples and helps you get a firm grip on the minute details, which ultimately leads to our problem.
◈ What Is A Syntax Error In Python?
Syntax errors occur when the Python compiler cannot understand the source code written by you and is unable to generate the machine code. Syntax error generally appear at compile-time and are reported by the interpreter.
Example: Incomplete if statement that lacks a colon at the end.
|
1 2 3 4 5 6 7 |
age = 25 if age<18 print('Not An Adult!') else: print('Adult!') |
Output:
◈ What does unexpected EOF while parsing mean in Python?
EOF is the abbreviation for End of File.
The EOFError is raised in situations where the end of a file is reached before running every block of code in the file.
Let’s visualize the following:
Most of you will find a glitch because you ran into an “unexpected end of paragraph” above. Not only does the paragraph above end mid-sentence, but also it ends mid-word! Now, imagine – Python is in a similar situation. That’s when it returns SyntaxError: unexpected EOF while parsing.
The following scenarios will help you to understand the occurrence of such errors and the ways to solve them.
✨ Scenario 1: Incomplete Loop/Function/If Statement
You must include at least one line of code within a For loop, While loop, if statements or functions; otherwise, it leads to the occurrence of Unexpected EOF error.
➥ Example 1: Unexpected End Of For Loop
|
1 2 3 4 |
lang = ['Java','Python','C','C++','Golang'] for i in lang: |
Output:
✍️ Solution:
You can use a print statement within the for loop body if you want to print the items of the lang list. You may also opt to use the pass statement if you do not wish to print anything and also avoid the error.
|
1 2 3 4 5 |
lang = ['Java','Python','C','C++','Golang'] for i in lang: print(i) |
Output:
➥ Example 2: Unexpected End Of Function
|
1 2 3 4 5 |
def foo(): return 'I love Java2Blog!' def func(): |
Output:
✍️ Solution:
The above error occurred because Python found an unexpected end to the function func(). Therefore you can avoid this error by using a pass statement within the function or by using a print statement to print something that meets your requirement.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def foo(): print('I love Java2Blog!') def func(): pass foo() func() # prints nothing |
Output:
✨ Scenario 2: Missing Parenthesis
If you forget to close all the parenthesis in a line of code within your program, then Python will raise the SyntaxError: unexpected EOF while parsing.
➥ Example 1:
|
1 2 3 4 5 6 7 |
website = 'Java2Blog' author = 'Shubham Sayon' founder = 'Arpit' print("The founder of {} is {} and its lead author is {}".format(website, founder, author) |
Output:
✍️ Solution:
The above error occurred because of a minute syntactic mistake wherein we forgot to close the print statement using a closing parenthesis ). To solve the error you simply have to close the print statement properly.
|
1 2 3 4 5 6 7 |
website = 'Java2Blog' author = 'Shubham Sayon' founder = 'Arpit' print("The founder of {} is {} and its lead author is {}".format(website, founder, author)) |
Output:
➥ Example 2:
Let us have a look at another case where a dictionary is incomplete and Python raises SyntaxError: unexpected EOF while parsing.
|
1 2 3 4 5 6 7 |
dict = { 'blog': 'Java2Blog', 'lang' : 'Python', print(dict['blog']) |
Output:
✍️ Solution:
Close the dictionary using the closing parenthesis to avoid the error.
|
1 2 3 4 5 6 7 8 |
dict = { 'blog': 'Java2Blog', 'lang': 'Python', } print(dict['blog']) |
Output:
✨ Scenario 3: Using try without except/finally
You will encounter the SyntaxError: unexpected EOF while parsing if you define a try block. However, you do not have an except or finally block.
➥ Example:
|
1 2 3 4 |
try: print("Hello Folks!") |
Output:
✍️ Solution:
To overcome this error, you have to define an except or finally block corresponding to the try block.
|
1 2 3 4 5 6 7 |
try: print("Hello Folks!") finally: pass |
Output:
✨ Scenario 4: Using the eval() function on str()
Python does not permit the usage of eval() function on str() and it leads to the SyntaxError: unexpected EOF while parsing.
➥ Example:
|
1 2 3 4 5 6 |
text1 = 'a string' text3 = eval(str(text1)) if text1 == text3: print("eval() Works!") |
Output:
✍️ Solution:
To avoid the above error you can replace the str() function with the repr() function.
|
1 2 3 4 5 6 7 8 |
text1 = 'a string' text3 = eval(repr(text1)) if text1 == text3: print("eval() Works!") |
Output:
Conclusion
To summarize our discussion, “SyntaxError: unexpected EOF while parsing” error in Python occurs when Python reaches the end of execution abruptly before every line of code has finished its execution. This happens when:
- The code has an incomplete Loop/Function/If Statement.
- The code has a
tryblock but nofinallyorexceptblocks. - You are trying to implement
eval()onstr().
To avoid this error, you should ensure that all the statements within your code are complete and have proper opening and closing parenthesis. Also, make sure that you define an except or finally block if the code has a try block.
I hope this article was helpful. Please subscribe and stay tuned for more exciting articles. Happy Learning! 📚