python – Strange error about invalid syntax

python – Strange error about invalid syntax

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

returnwas invalid syntax

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

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

Hope this helps.

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 doesnt 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.

python – Strange error about invalid syntax

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.

Leave a Reply

Your email address will not be published. Required fields are marked *