Can not get past illogical line pep8 error

Question:

I’ve been trying to fix this for a while now and I just can’t get it to pass pep8.
Here is my code:

1.

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and 
    sum(regex.count(char) for char in splitter) == 1 and 
    regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')
if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and 
    sum(regex.count(char) for char in splitter) == 1 and 
    regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')
if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' 
    and regex.count('(') > 1):
        
    print('hi')

I get the following PEP8 error on each of the 3 if statements:

E125 continuation line does not distinguish itself from next logical line

Any idea on what’s wrong with it? The lines are indented with parenthesis so i really don’t have any clue.

Asked By: user3050527

||

Answers:

I’m using PyCharm (which is pretty good for pointing out PEP8 errors) for my editing, and it says this version is ok:

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and
        sum(regex.count(char) for char in splitter) == 1 and
        regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')
Answered By: Steinar Lima

1.

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and
        sum(regex.count(char) for char in splitter) == 1 and
        regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')

2.

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and
        sum(regex.count(char) for char in splitter) == 1 and
        regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')

3.

if (len(regex) > 2 and regex[0] == '(' and regex[-1] == ')'
        and regex.count('(') > 1):

    print('hi')
Answered By: user3412839

I’m not saying I love this solution, but I think that removing the space after if is less of a compromise than lining up the second line with the guts of the len call, like the other answers here suggest:

if(len(regex) > 2 and regex[0] == '(' and regex[-1] == ')' and
   sum(regex.count(char) for char in splitter) == 1 and
   regex.count('(') == 1 and regex.count(')') == 1):

    print('hi')
Answered By: Jim Hunziker
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.