Python is a statically typed language. This means it’s strict about how code is written.
If you forget to complete a code block in your code, you get an error like “SyntaxError: unexpected EOF while parsing”. This happens in a number of situations, such as when you forget to add a line of code into a for loop.
In this guide, we talk about this Python error and why it is raised. We walk through a few example scenarios so you can figure out how to solve this common error.
SyntaxError: unexpected EOF while parsing
The “SyntaxError: unexpected EOF while parsing” error occurs when the end of your source code is reached before all code is executed. This happens when you make a mistake in the structure, or syntax, of your code.
EOF stands for End of File. This represents the last character in a Python program.
Python reaches the end of a file before running every block of code if:
- You forget to enclose code inside a special statement like a for loop, a while loop, or a function.
- You do not close all of the parenthesis on a line of code in your program.
Let’s walk through each of these mistakes one-by-one. There are other scenarios where this error is raised but those mentioned above are the most common.
Example #1: Enclosing Code in a Special Statement
For loops, if statements, while loops, and functions require at least one line of code in their statements. Forgetting to include a line of code in a special statement will result in an unexpected EOF error.
Take a look at a for loop that prints out a list of ingredients in a recipe:
ingredients = ["325g plain flour", "200g chilled butter", "125g golden caster sugar", "2 tsp vanilla extract", "2 free range egg yolks"] for i in ingredients:
We define a variable called “ingredients” that stores a list of ingredients for a vanilla shortbread recipe. We use a for loop to iterate through each ingredient in the list. Run our code and see what happens:
File "main.py", line 4 ^ SyntaxError: unexpected EOF while parsing
We have not added any code into our “for” loop. This raises an error. This same error occurs if we define a while loop, an if statement, or a function without enclosing any code in the statement.
To solve this problem, we add some code to our loop. We add a print()
statement so we can print each individual ingredient to the console:
for i in ingredients: print(i)
Let’s run our code:
325g plain flour 200g chilled butter 125g golden caster sugar 2 tsp vanilla extract 2 free range egg yolks
Our code prints out each ingredient in our list of ingredients. This tells us the code blocks were completed successfully.
If you do not have any code you want to add into a special statement, use the “pass” statement as a placeholder. Consider this code:
ingredients = ["325g plain flour", "200g chilled butter", "125g golden caster sugar", "2 tsp vanilla extract", "2 free range egg yolks"] for i in ingredients: pass
This code returns no values. We have defined a loop but the “pass” statement tells our program the loop does not need to do anything yet. This keyword is often used when developers are building the structure for a program. Once a program’s structure is determined, “pass” statements are replaced with the relevant code.
Example #2: Unclosed Parenthesis
An “unexpected EOF while parsing” error occurs when you forget to close all of the parenthesis on a line of code.
Write a program that prints out information about a recipe to the console. Start by defining a few variables with information on a recipe:
name = "Vanilla Shortbread" author = "Career Karma" vegetarian = "This recipe is vegetarian."
We format this into a string using the .format()
method:
print("The {} recipe was devised by {}. {}".format(name, author, vegetarian)
The {} values are substituted with the respective values in the .format()
statement. This means that our string will say:
The NAME recipe was devised by AUTHOR. VEGETARIAN
Run our code:
File "main.py", line 7 ^ SyntaxError: unexpected EOF while parsing
On our print()
line of code, we only close one set of parenthesis. We have opened two sets of parentheses. Hence, an error has been returned.
We solve this problem by adding an end parenthesis (“)”) character to the end of the print()
line of code:
print("The {} recipe was devised by {}. {}".format(name, author, vegetarian))
This line of code ends in two parenthesis instead of one. All parenthesis are now closed.
Let’s attempt to run our code again:
The Vanilla Shortbread recipe was devised by Career Karma. This recipe is vegetarian.
Our code runs successfully.
This same error happens if you forget to close a dictionary using the {} brackets. You also encounter this error if you forget to close a list using the [] brackets.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Conclusion
The “SyntaxError: unexpected EOF while parsing” error is raised when the Python interpreter reaches the end of a program before every line of code has been executed.
To solve this error, first check to make sure that every if statement, for loop, while loop, and function contains code. Second, check to make sure you close all the parenthesis in your code.
Now you’re ready to solve this syntax error like a Python professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
I forgot a parenthesis and didn’t notice. I spent an hour trying to fix this error. You saved my ass.