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 (2016, retired 2019) Slices Slice Functions

what's wrong with my code

this code is running well on my python idle but it's not accepted on the challenge

slices.py
def first_4(item):
    return item[:4]
def first_and_last_4(things):
    al = things[:4] + things[-4:]
    return al
def odds(numbers):
    me = numbers[1::2]
    return me
def reverse_evens(shalo):
    ibu = shalo[::-1]
    return ibu[0::2]

4 Answers

Steven Parker
Steven Parker
231,275 Points

After reversing the list, which indexes were originally "even" will be based on the length of the list. It might work, but only half of the time.

Try slicing the even indexes first and then reversing them.

Hie! conditional branching will do you good here. check below:

def reverse_evens(shalo):
    ibu = shalo[::-1]
    if len(ibu)%2==0:
        even=ibu[-2::-1]
    else:
        even=ibu[-1::-1]

    return even

This really works

Steven Parker
Steven Parker
231,275 Points

There are two basic strategies for solving this task:

  • extract the evens first and then reverse them
  • compute the reverse starting position based on the even/odd-ness of the list size

Both methods work equally well when implemented correctly (but the first takes less code).

You are right Steve! Iwas just giving an extra suggestion for fun.

Timmie Nilsson
Timmie Nilsson
4,463 Points

None of this thing works according to the code challange engine... I have been trying them out @ the workplace and there they work fine...

Steven Parker
Steven Parker
231,275 Points

It's easy to get a "false positive" working outside the challenge, either because of misunderstanding the objective, or testing with "easy data" that doesn't cover the same cases the challenge will.