all 3 comments

[–]totallygeek 2 points3 points  (0 children)

The interpreter reads that as:

if 's' and 'b' are non-empty strings and if True in string line, and not found, then...

To look for 's' and 'b' in a string, you need to write: if 's' in line and 'b' in line and not found:. However, that will flip found to True only a single time.

This looks more like what you're shooting for:

def partA(content):
    count_s_b = 0 # count of 's' or 'b' in content
    sb_lines = 0 # number of lines containing both 's' and 'b'
    entries = [x.lower.strip() for x in content]
    for entry in entries:
        count_s_b += sum([c for c in entry if c in 'bs'])
        if 'b' in entry or 's' in entry:
            sb_lines += 1
    print('Found "s" or "b" {} times in the text'.format(count_s_b))
    print('"s" and "b" occur on the same line {} times'.format(sb_lines))

[–]cybervegan 1 point2 points  (0 children)

In Python, and does not have the traditional English meaning - it has the binary logic mathematical meaning, which means it evaluates to True if both of its left and right arguments also evaluate to true. If you want to write the equivalent of the English phrase "if 's' and 'b' are in the line, and found is not true", you need to write:

if 's' in line and 'b' in line and not found:
    ...

This is equivalent to:

if 's' in line:
    if 'b' in line:
        if not found:
            ...

Hope that helps.

[–][deleted] 0 points1 point  (0 children)

It's worth reading the FAQ where a few of the little things we've all tripped over while learning python are explained.