Search code examples
pythonpython-2.7

Get Rid of SyntaxError: invalid syntax in Python


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

No idea what is happening here..

Code:

 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)

Solution

  • The function you posted is valid Python 3.7. Type hints were added to Python in version 3.5.0, the code you posted should work in any version from 3.5.0 onwards:

    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>
    

    I suspect you are using an older python version that does not support these language features.