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 Sequences Sequence Operations Creating Slices

Python Sequences Challenge Task 1 of 1.

"Create a slice of the provided student_gpas list that includes the 3rd through the 5th elements in the list. Assign the slice to a variable called sliced_gpas."

I had an issue with this code challenge but solved it after trying numerous solutions. I feel like the details are not clear and concise. Coming from a no programming background with little to no experience, the general person will not have a problem-solving mindset.

The problem would make sense if it read create a variable named 'sliced_gpas' assigned to student_gpas with a sliced index of 3-5.

The next issue I had, if it is requesting indexes 3-5 why wouldn't the input of [3:6] work?

student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0]

sliced_gpas = student_gpas [3:6]

print(sliced_gpas)
OUTPUT = [3.7, 3.9, 2.8] 


#The following code above correct me if Iā€™m wrong are indexes 3-5.


student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0]

sliced_gpas = student_gpas [2:6]

print(sliced_gpas)
OUTPUT = [3.5, 3.7, 3.9, 2.8] 
#The following indexes are 2-5.


#Create a slice of the provided student_gpas list that includes the 3rd through the 5th elements in the list. Assign the slice to a variable called sliced_gpas.

student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0]

sliced_gpas = student_gpas [2:6]

5 Answers

student_gpas = [4.0, 2.3, 3.5, 3.7, 3.9, 2.8, 1.5, 4.0]

sliced_gpas = student_gpas [2:5]

This worked! Try it....

Christopher Sullivan
Christopher Sullivan
4,230 Points

Just had the biggest "Ohhhhh" when I read this "The problem would make sense if it read create a variable named 'sliced_gpas' assigned to student_gpas with a sliced index of 3-5.". Makes a lot more sense. Even though I just watched the videos, I should have known that to catch the desired sets you have to go up 1 more number.

Josh Keenan
Josh Keenan
19,652 Points

I believe the challenges for a lot of the new courses are a bit broken, I had the same issue and the code that passes is not actually the right code for what they are asking, i just slowly expanded the range and tweaked it after I realised.

Submit a bug report, i think there are some issues with the new python material that needs solving, had 3 challenges if I recall correctly that had something wrong.

Anthony Giordano
Anthony Giordano
1,783 Points

sliced_gpas = student_gpas [2:5]

because the index is 0,1,2 = 3rd element. 0,1,2,3,4, = 5 elements but remember it stops at the one before so you need to put a value of 5 for the stop, because if you were to put 4 it would actually stop at the 3rd.

bessiebarnes
bessiebarnes
88,151 Points

Download the example of slices in Workspaces, the answer for this challenge is there. Here is an example:

colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] colors_partial = colors[3:6] print(colors_partial)