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 trialDaniel Smith
10,172 PointsI'm not sure why my reverse evens function isn't working.
The code works in pycharm, perhaps it wasn't expecting the modulus check?
def first_4(word):
return word[0:4]
def first_and_last_4(word):
return word[:4]+ word[-4:]
def odds(word):
return word[1::2]
def reverse_evens(word):
if len(word)%2==0:
return word[-1::-2]
else:
return word[-2::-2]
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsYou are close. The if
condition is inverted. Should be:
len(word) % 2 != 0
That would pass but there is an easier way. What if you use to steps: get the even indexed item, then reverse them?
Post back if you need more help. Good luck!!!
Ed H-P
2,728 PointsEd H-P
2,728 PointsEDIT1:
I've just realised I haven't specified the starting index (in this case the final index) is even OR odd.
This is how I re-wrote it:
--
Hi Chris,
I got the first three functions accepted, but it didn't allow my
reverse_evens
function. I checked it on my IDLE Python 3.6 Shell and it worked; I got [5,3,1] as the output.Any suggestions?
TIA,
Ed
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsEd H-P, I looks like your condition is reversed
Also, it is recommended to avoid using the name of built-in functions and types, such as
input
, as variable labels.Ed H-P
2,728 PointsEd H-P
2,728 PointsChris you are absolutely correct - I'd mistakenly returned the even numbers instead of even indices. Thanks for the suggestions!