摆脱Python中的SyntaxError: invalid syntax错误

3
def success(self, input1: str = "", input2: str = "", input3: str = "") -> None:
E                               ^
E   SyntaxError: invalid syntax

不知道这里发生了什么...

代码:

 def success(self, input1: str = "", input2: str = "", input3: str = "") -> None:
        input1 = str(input1)
        input2 = str(input2)
        input3 = str(input3)
        print(Color.BOLD + Color.GREEN + " " + Color.CHECKMARK + " " + input1 + Color.END + " " + input2 + " " + input3)

6
你使用的是哪个Python版本?参数类型提示是一个相对较新的添加。 - Prune
我想你是正确的。本来应该看到它的。它正在使用Python 2.7,但我很确定我需要更新的版本。我在Mac上。我已经安装了所有内容,但不知道如何更正,似乎是不同的问题。 - Stephan Kristyn
2个回答

5

由于您使用的是Python 2.7版本,因此需要回退到该语法。类型提示是Python 3的特性,请将其删除。

def success(self, input1="", input2="", input3=""):

3

您发布的函数是有效的Python 3.7代码。类型提示在Python 3.5.0版本中被添加,您发布的代码应该可以在从3.5.0版本开始的任何版本上工作:

Python 3.7.0 (default, Aug 22 2018, 20:50:05) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def success(self, input1: str = "", input2: str = "", input3: str = "") -> None:
...         input1 = str(input1)
...         input2 = str(input2)
...         input3 = str(input3)
...         print(Color.BOLD + Color.GREEN + " " + Color.CHECKMARK + " " + input1 + 
Color.END + " " + input2 + " " + input3)
... 
>>> success
<function success at 0x7fb70e4c61e0>

我怀疑您正在使用旧版本的Python,不支持这些语言特性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接