Syntax error in if statement Python
Question: For some reason I get a syntax error on this function.

    
    
    def log(user, successful):
    if successful == True:
        with open('userlog.txt', 'a') as logfile:
            logfile.append(datetime.datetime + ' User ' + user + ' Logged in'
    else:
        with open('userlog.txt', 'a') as logfile:
            logfile.append(datetime.datetime + ' Unsuccessful login attempt as ' + user) )
    

Here is the error

    
    
        Traceback (most recent call last):
      File "C:\conf.py", line 4, in <module>
        import func
      File "C:\func.py", line 48
        else:
           ^
    SyntaxError: invalid syntax
    

Now, i've seen other posts that say I should add an extra parenthesis when
this happens. I tried it and it didnt work. Thanks in advance.


Answer: It looks like you copy/pasted some code wrong. This line needs a closing
parenthesis:

    
    
    logfile.append(datetime.datetime + ' User ' + user + ' Logged in'
    #                                                                ^
    

and this one has an extra closing parenthesis:

    
    
    logfile.append(datetime.datetime + ' Unsuccessful login attempt as ' + user) )
    #                                                                            ^
    

Simply move the extra parenthesis from the second line to the first and all
will be well.

* * *

Actually, you have one more problem. As @MartijnPieters said in the comments,
you cannot concatenate a `datetime.datetime` object with strings.

I _think_ you actually meant to do:

    
    
    logfile.append(str(datetime.datetime.now()) + ' Unsuccessful login attempt as ' + user)
    

This will return the current time as a string, which can then be concatenated
with other strings:

    
    
    >>> import datetime
    >>> str(datetime.datetime.now())
    '2014-12-15 13:03:49.242187'
    >>>
    

