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

Saloni Gandhi
Saloni Gandhi
1,531 Points

slices.py

def first_4(new):
  new2=new[0:4]
  return new2
def odds(sample):
   new3= sample[::-2]
    new3.reverse()
  return new3

it works perfectly when i tried it in the terminal but it gives me an error

[MOD: added ```python markdown formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You had some indention errors in odds, plus the indexing was off. Here is your corrected code:

def first_4(new):
  new2=new[0:4]
  return new2
def odds(sample):
  new3= sample[1::2]
  #new3.reverse()
  return new3

Not all iterables (such as strings) have a reverse() method. Your original odds would throw an error if a string was passed as an argument.