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 trialAndrew McLane
3,385 Pointshow to format function to allow for strings.
I got the code to work for lists of numbers, but I can't figure out how to allow for lists that include strings, such as a list of letters.
I need to be able to determine if the last index of a list is even or odd, but I can only think of a way to do so if the list is of numbers.
def first_4(iterable):
return iterable[0:4]
def first_and_last_4(iterable):
return iterable[0:4] + iterable[-4:]
def odds(iterable):
return iterable[1::2]
def reverse_evens(iterable):
if iterable[-1] % 2 == 0:
return iterable[::-2]
else:
return iterable[:-1:-2]
2 Answers
Afloarei Andrei
5,163 PointsTry to do it like this: iterable[][*]
*select only the even numbers **invert the numbers
Andrew McLane
3,385 PointsThanks, I'd like to solve the problem using methods I've learned up to this point, so that I fully understand how they work. I'm not even sure how to input those symbols.
Afloarei Andrei
5,163 PointsYou used those symbols in the challenge: if you want to print the even numbers you do something like in you'r odd function but slightly different. ex: iterable[1::2] is printing the item from the 1 index and it jumps the next and prints the item from index 3. with that in mind make the function to print the even numbers. and the invert list is [::-1] from the videos. Finally put those 2 together and you get something like in my commet.
iterable[even numbers][invert list]
Afloarei Andrei
5,163 PointsAfloarei Andrei
5,163 Pointsiterable[even numbers][invert list]