Главная » Error » Unexpected EOF While Parsing

Unexpected EOF While Parsing

What is unexpected EOF while parsing error?

The SyntaxError: unexpected EOF while parsing error also is known as parsing error where the control in the program/code reaches to the end, but there are more code/program lines that are needed to be compiled. It can be due to some incomplete syntax, i.e.,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 any single wrong statement not due to block of code. 

Cause of SyntaxError: Unexpected EOF while parsing

This error can occur due to the following reasons.

  • Incomplete parameters may cause this kind of errors.
  • Incomplete functions along with the combination of keys 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 the compiler was compiling the code, it couldn’t 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 the ‘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

Conclusion

In this article, we covered what causes the Python error “SyntaxError: unexpected character after line continuation character”. It’s always caused by something coming after a backslash that’s not within a string. Inside a string, the character is used to denote a special character, like n. Outside a string, like in your regular Python code, it’s a continuation character, which means it tells the Python interpreter that the current line is continued on the next one. It can’t have anything after it, or you’ll get this error.

We also discussed why you should use a text editor (or IDE) with syntax highlighting . This will save you a lot of trouble in the long run.

That’s all for now, happy coding!

TypeError: write() argument must be str, not bytes

TypeError: write() argument must be str, not bytes

❌ Problem: you didn’t put a string containing a special character in quotes

You can also easily have this problem if you forget one or more quotes in a string that contains a special character, such as t or n:

my_string = tfoo”

print(my_string)

As you can see here, all we wanted to do was have a string containing the word “foo” that is tabbed over once (like this: ” foo”). But, instead, we get the following error message:

File “/home/user/main.py”, line 1
my_string = tfoo”
^
SyntaxError: unexpected character after line continuation character

Whoops!

✅ Fix: add the missing quote(s)

This one’s an easy fix too. All we have to do is add the missing quotation marks and it should work as expected:

my_string = “tfoo”

print(my_string)
# -> foo

That’s all there is to it!

☝️ Tip: use syntax hilighting!

This should come as no surprise, but you can avoid mistakes like this by using a text editor or integrated development environment (IDE) that has syntax highlighting like the code blocks you see above do. If you’re stuck on a system that has vanilla vi installed or something, then I guess you’re out of luck. But, if you have the ability to do so, try:

  • vim – this might already be installed on the system you’re editing Python code on – try it and see! You can save yourself a lot of hassle. If you’re on a Debian based Linux disto such as Ubuntu, and you’re in the sudoers list, you can install it with sudo apt-get install vim.
  • PyCharm – this is a great, full-featured Python IDE by JetBrains. Don’t let the price tag scare you, there’s a free version called “Community Edition” or “CE”. Install that one and it won’t harass you for a license key when you go to install it
  • Atom – this one’s a personal favorite. It’s an all-purpose text editor with tons of plugins, similar to Notepad++ on Windows systems, or Sublime Text on Mac. But, this one’s cross-platform and best of all, it’s free. Which is my favorite price!

❌ Problem: you accidentally put something after the backslash

We’ll start with the most obvious one: you did just make a mistake. I know, I know. You don’t make mistakes. I don’t either. It was probably someone else. But now it’s your problem, because it’s 4:58 on a Friday afternoon and your branch won’t merge because of a syntax error.

Here’s an example of some code that will cause this problem:

my_string = “This is a string n” n
“That is on multiple linesn”

print(my_string)

This is the same example from above, except there’s a stray n after the . If you run this code, you’ll get the following stack trace:

File “/home/user/main.py”, line 1
my_string = “This is a string n” n
^
SyntaxError: unexpected character after line continuation character

As you can see, the Python interpreter is pointing right at the problem. It’s easy to make this particular mistake, because you were already putting ns in your string, so you may have slipped up and accidentally put one after the continuation character as well. Or someone else did. But we both know it was you. Don’t worry, I’m no snitch.

This can also happen if you didn’t actually put the second line on a new line:

my_string = “This is a string n” “That is on multiple linesn”
# ^^^ This is also wrong! ^^^

✅ Fix: just remove the stuff after the backslash or put it on a new line

This one is easy to fix: just delete the extra stuff or put it on a new line like so:

my_string = “This is a string n”
“That is on multiple linesn”

print(my_string)

# -> This is a string
# That is on multiple lines

Now we’re back in business!

How to resolve this Error?

To avoid this error, 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

❌ Problem: you’re using a backslash for division (why?)

This is a weird one, but I’ve seen it happen before. Not me of course, I would never do this. But someone has. For the sake of completeness, here’s what the wrong thing looks like:

print(12)

This, as you can imagine, isn’t going to give you 0.5. It’s going to give you a stack trace. And that stack trace is going to look like this:

File “/home/user/main.py”, line 1
print(12)
^
SyntaxError: unexpected character after line continuation character

No surprises here. Let’s fix this.

✅ Fix: use a forward slash for division ‍♂️

Alright kids, gather ’round. Grandpa Bill is going to lay down a life lesson for you: you divide with a forward slash. Like this:

print(1/2)
# -> 0.5

And there you have it.

Источники

  • https://www.stechies.com/syntaxerror-unexpected-eof-while-parsing/
  • https://badcodernocookie.com/syntaxerror-unexpected-character-after-line-continuation-character/
[свернуть]
Решите Вашу проблему!


×
Adblock
detector