16. Comprehending Comprehensions


The code I used seems to have produced the correct answer, but I am getting the following error: > "Oops, try again. threes_and_fives contains 1, but shouldn't"

Here is my code:

threes_and_fives = range(1,16)
print filter(lambda x: x % 3 ==  0 or x % 5 == 0, threes_and_fives)

The console shows:

Thanks for any help!


Here's what's going wrong. You code is tested by looking what's in threes_and_fives and not what is printed. Since you don't modify threes_and_fives it'll be just a list of 1 to 15.


Thnx cookiebot - it threw me off that my answer was right, but the way that I arrived at it was wrong. Finally got it!


All I did was

threes_and_fives = [x for x in range(1,16) if x % 3 == 0 or x % 5 == 0]


Why does and not work?

threes_and_fives = [x for x in range(1, 16) if x % 3 == 0 and x % 5 == 0]


I came to this thread to suggest the same thing. I then added a print statement to verify the code. Worked.


I think its not working because you put "...x%3==0 AND x%5==0..." And means its looking for the inputs to be both divisible by 3 and 5. You are looking for ones that are divisible by 3 OR 5.


thanks for pointing out the "and" and "or"