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 trialJennefer Boudreau
3,423 PointsPython Slices Challenge 1: What am I doing wrong?
Okay, guys and gals! My brain can't wrap around this problem and what I am doing wrong. I counted the indexes numerous times and pretty sure I checked my spelling. I keep getting a "Bummer! Didn't find the right items blah blah blah"
I need to include the 2nd, 3rd, and 4th items.
I created a variable that copies the list "favorite_things" and now I need to take that copy and show the above items.
What am I doing incorrectly?!
favorite_things = ['raindrops on roses', 'whiskers on kittens', 'bright copper kettles',
'warm woolen mittens', 'bright paper packages tied up with string',
'cream colored ponies', 'crisp apple strudels']
slice1 = favorite_things[:]
slice1[1:5]
Jennefer Boudreau
3,423 PointsNevermind! I figured it out!
BUT, can someone explain why I needed to type it like this:
slice1 = favorite_things[:] slice1 = slice1[1:4]
Wouldn't slice[1:4] still print out the intended list items after creating the variable slice1?
Alexander Davison
65,469 PointsYou didn't need the slice1 = favorite_things[:]
line of code.
All you need is:
slice1 = favorite_things[1:4]
1 Answer
Alexander Davison
65,469 PointsYou don't need to make a copy of the list because you are making a new list from scratch.
If you write something like this:
a = [1, 2, 3]
b = a[0:2]
a
and b
are separate, because a[0:2]
makes a brand new list.
So, this means you only need to write this to pass the challenge:
slice1 = favorite_things[1:4]
Jennefer Boudreau
3,423 PointsThank you! Less typing is always good =]
Jennefer Boudreau
3,423 PointsJennefer Boudreau
3,423 PointsBtw, I have also tried [1:4]
I noticed the the above [1:5] is not correct, but [1:4] returns an error as well.