开发者

Strange error about invalid syntax

I am getting invalid syntax error in my python script for this statement

44 f = open(filename, 'r')
45 return

 return
    ^
SyntaxError: invalid syntax

I am not sure what exactly is wrong here? I am a python newbie and so开发者_JAVA技巧 will greatly appreciate if someone can please help.

I am using version 2.3.4


I had the same problem. Here was my code:

def gccontent(genomefile):
    nbases = 0
    totalbases = 0
    GC = 0
    for line in genomefile.xreadlines():
        nbases += count(seq, 'N')
        totalbases += len(line)
        GC += count(line, 'G' or 'C')
    gcpercent = (float(GC)/(totalbases - nbases)*100
    return gcpercent

'return'was invalid syntax

I simply failed to close the bracket on the following code:

gcpercent = (float(GC)/(totalbases - nbases)*100

Hope this helps.


I got an "Invalid Syntax" on return when I forgot to close the bracket on my code.

elif year1==year2 and month1 != month2:
    total_days = (30-day1)+(day2)+((month2-(month1+1))*30   
    return (total_days)    

Invalid syntax on return.

((month2-(month1+1))*30  <---- there should be another bracket

((month2-(month1+1)))*30

Now my code works.

They should improve python to tell you if you forgot to close your brackets instead of having an "invalid" syntax on return.


Getting "invalid syntax" on a plain return statement is pretty much impossible. If you use it outside of a function, you get 'return' outside function, if you have the wrong indentation you get IndentationError, etc.

The only way I can get a SyntaxError: invalid syntax on a return statement, is if in fact it doesn't say return at all, but if it contains non-ascii characters, such as retürn. That give this error. Now, how can you have that error without seeing it? Again, the only idea I can come up with is that you in fact have indentation, but that this indentation is not spaces or tabs. You can for example have somehow inserted a non-breaking space in your code.

Yes, this can happen. Yes, I have had that happen to me. Yes, you get SyntaxError: invalid syntax.


i just looked this up because i was having the same problem (invalid syntax error on plan return statement), and i am extremely new at python (first month) so i have no idea what i'm doing most of the time.

well i found my error, i had forgotten an ending parentheses on the previous line. try checking the end of the previous line for a forgotten parentheses or quote?


>>> 45 return
  File "<stdin>", line 1
    45 return
            ^
SyntaxError: invalid syntax
>>> 

That might explain it. It doesn't explain the 44 f = open(filename, 'r'), but I suspect that someone copied and pasted 45 lines of code where the indentation was lost and line numbers included.


Usually it's a parenthetical syntax error. Check around the error.


I encountered a similar problem by trying to return a simple variable assignation within an "if" block.

    elif len(available_spots)==0:
    return score=0 

That would give me a syntax error. I fixed it simply by adding a statement before the return

 elif len(available_spots)==0:
    score=0
    return score


I had the same problem. The issue in my case was, that i mixed up datatypes in my return statement. E.g.

return string + list + string
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜