代码之家  ›  专栏  ›  技术社区  ›  Stephan Kristyn

消除语法错误:python中的语法无效

  •  2
  • Stephan Kristyn  · 技术社区  · 6 年前
    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)
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   John Lyon    6 年前

    您发布的函数是有效的python 3.7。类型提示 added to Python in version 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版本。

        2
  •  4
  •   Prune    6 年前

    因为您使用的是Python2.7,所以需要备份到该语法。类型提示是python 3;删除它们。

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