"Practice Makes Perfect" not accepting correct answer


#1



https://www.codecademy.com/en/courses/python-beginner-en-KAgt5/2/5?curriculum_id=4f89dab3d788890003000096


My code itself has no error messages and I use list slicing to create the lists requested. Below is the output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
[8, 9, 10, 11, 12, 13, 14]
None

However, I get a message stating the following:

"Oops, try again. Make sure to use list slicing in your answers. "

What am I missing here?

I expect the codecademy grading bot to accept an answer that I think is correct.


to_21 = range(1,22)
print to_21

odds = [i for i in to_21 if i % 2 == 1]
print odds

middle_third = [i for i in to_21 if (float(i/21.000) > float(1.000/3.000) and float(i/21.000) <= float(2.000/3.000))]
print middle_third


#2

Never mind. I simply should replace "odds = [i for i in to_21 if i % 2 == 1]" with "odds = to_21[::2]". My bad


#3

They said not to use list comprehension so basically you have to use List Slicing,

odds = to_21[::2]

#Output => [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]

Here as well,

middle_third = to_21[7:14]

#Output => [8, 9, 10, 11, 12, 13, 14]


#4

this:

middle_third = [i for i in to_21 if (float(i/21.000) > float(1.000/3.000) and float(i/21.000) <= float(2.000/3.000))]

is list comprehension, not list slicing, good you figured it out :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.