Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Slices Slice Functions

Kishore Kumar
Kishore Kumar
5,451 Points

Collection

Make a function named odds that accepts an iterable as an argument and returns the items with an odd index in the iterable.

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

When it says "Task 1 is no longer passing" it means a syntax error was introduced throwing off checking of all tasks. I ran your code and got a "wrong results" message not a Task 1 is failing. For Task 2, adjusting your code, you need to operate on the argument passed in.

def first_4(iterable):
    return iterable[0:4]

def odds(iterable):
  return iterable[1::2]

For the challenges there isn't a need to call your functions. The grader will do that.

Ah, this isn't intuitive, so you have to catch it in the video or read the docs to figure it out. A Slice has the following syntax:

list[start:end:step]

So, if you leave the first spot empty, you start at zero. But in your case, you would need to start at 1. You can leave the 'end' section empty because you want it to go through the entire iterable. The 'step' section lets you decide by how many numbers you want to jump after each slice. The default is 1, but you would need to enter 2 to make sure you skip every other number.

list[1::2]

Does that make sense?

hint: you would have to use a very similar setup to get even numbers. I suggest you play with different arrangements to get a more intuitive feel for Slicing things. Have fun!

Kishore Kumar
Kishore Kumar
5,451 Points

my Code from Challenge-1 to challenge-2:

def first_4(iterable):
  i = list(range(25))
  return i[1:5]

first_4(25)

def odds(iterable):
  i = list(range(20))
  return i[1::2]

odds()

and it gives me the error message: Oops! It looks like Task 1 is no longer passing.

Please suggest the entire code