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 trialDotan Sharaby
652 Pointsdid i write my slice wrong?
im not sure this is the right way to go to begin with..would appreciate any help :)
def first_4(range1):
f4 = range1[0:4]
return f4
def first_and_last_4(rango):
b = rango[0:4]
c = rango[-4:]
d = b + c
return d
def odds(neg):
return neg[1::2]
def reverse_evens(inter):
if inter %2 == 0:
return inter[::-2]
else:
return inter[-1::-2]
3 Answers
Steven Parker
231,236 PointsYou're close, but I see two issues:
- the remainder (%) won't work on a list, you probably want to use the length of the list
- if the length is even, the slice should begin with the 2nd to last item
Alex A.
1,279 PointsHello Dotan, I believe your error is in your last function. Your function parameter(inter) will be expecting a list argument. So when you say "if inter % 2 == 0", you're not making sense, because you can't use the modulus symbol on a list.
In fact, the way you're using the modulus symbol is redundant in this scenario, because the slice function can provide the exact same result you're aiming for, that is, to increment in steps of 2.
Unfortunately I can't specifically remember what this task is asking you to do but judging by your function name, I believe you're attempting to return each even index of a passed through list in reverse?
This can be accomplished as follows:
def reverse_evens(inter):
inter_copy = inter[::2] # <-- This assigns a copy of the evens of the list to a new variable.
return inter_copy[::-1] # <-- This then returns the reverse of this list.
Hopefully this makes sense, if you need any further assistance please follow up with a message. //- Alex
Shawn Rodgers
7,158 Pointsdef reverse_evens (iterable) : iterable = list(iterable) return iterable[::-2]
Dotan Sharaby
652 PointsDotan Sharaby
652 Points"Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse.
For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1]."