Python 关于无效语法的奇怪错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4991051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:29:52  来源:igfitidea点击:

Strange error about invalid syntax

python

提问by Sumod

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

对于此语句,我的 Python 脚本中出现无效语法错误

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 will greatly appreciate if someone can please help.

我不确定这里到底有什么问题?我是一个 python 新手,如果有人能帮忙,我将不胜感激。

I am using version 2.3.4

我使用的是 2.3.4 版

采纳答案by Lennart Regebro

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.

在简单的 return 语句上获得“无效语法”几乎是不可能的。如果在函数之外使用它,则会得到'return' outside function,如果缩进错误,则会得到IndentationError,等等。

The only way I can get a SyntaxError: invalid syntaxon a return statement, is if in fact it doesn't say returnat 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.

我可以SyntaxError: invalid syntax在 return 语句上获得 a 的唯一方法是,如果实际上它根本没有说return,但是如果它包含非 ascii 字符,例如retürn. 那给这个错误。现在,你怎么能没有看到这个错误呢?同样,我能想到的唯一想法是你实际上有缩进,但这个缩进不是空格或制表符。例如,您可以以某种方式在代码中插入一个不间断空格。

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

是的,这可能发生。是的,我遇到过这种情况。是的,你明白了SyntaxError: invalid syntax

回答by S.Lott

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

这或许可以解释。它没有解释44 f = open(filename, 'r'),但我怀疑有人复制并粘贴了 45 行代码,其中缩进丢失并包含行号。

回答by Jonathan

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

'return' 是无效的语法

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

我只是未能关闭以下代码的括号:

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

Hope this helps.

希望这可以帮助。

回答by Jared

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

通常是括号语法错误。检查周围的错误。

回答by Thuy

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.

他们应该改进 python 以告诉您是否忘记关闭括号,而不是在返回时使用“无效”语法。

回答by emmet

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.

我只是查了一下,因为我遇到了同样的问题(计划返回语句中的语法错误无效),而且我对 python 非常陌生(第一个月),所以我大部分时间都不知道我在做什么。

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?

好吧,我发现了我的错误,我忘记了前一行的结尾括号。尝试检查上一行的末尾是否有忘记的括号或引号?

回答by JFT

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

我试图在“if”块中返回一个简单的变量赋值,遇到了类似的问题。

    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