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 sillyCase

What does this error mean?

I am trying to grab the first half of the list. Everything was going great until this error came by: "TypeError: slice indices must be integers or None or have an index method". Why can't I use the (len(this)/2) as a stop? What should I do instead?

def sillycase(string):

this = (list(string)) print(this) print("".join(this))

print(this[:(len(this)/2)])

2 Answers

Anish Walawalkar
Anish Walawalkar
8,534 Points
len(this)/2 # returns a float. e.g 5/2 = 2.5
len(this)//2 #return a int, e.g. 5//2 = 2
Travis Bailey
Travis Bailey
13,675 Points

It's likely caused by the math you're doing in your slice resulting in a float. Use the round() function to round the float to the nearest whole number. Slice indices can't be floats.