16. Comprehending Comprehensions


#1

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:
[3,5,6,10,12,15]
None.

Thanks for any help!


#2

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.


#3

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


#4

All I did was

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

#5

Why does and not work?

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


#6

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


#7

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.


#8

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