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

William Warren
2,519 Pointspython len method doesn't return an int?
I am trying to divide a string using a slice, however
'''python firsthalf= word[:len(str) / 2] secondhalf = word[len(str) / 2:] '''
throws an error asking for slice indices to be ints
2 Answers

andren
28,558 Pointslen by itself returns and int
but division produces floats (numbers with decimals), which is a number type that is separate from int.
So you can end up with numbers like 2.5 which doesn't make sense as a slicing value. One of the easiest ways around this is to use the round
method on the division operation, round
takes a float and rounds it to an int value.

William Warren
2,519 Pointsthanks!
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsIf this is for the sillycase challenge then you'll need to convert it to an int.
round()
won't give the correct index every single time.The decimal has to be truncated. You can either convert to an int with
int()
or do integer division with//
Examples:
andren
28,558 Pointsandren
28,558 PointsAh is that so, I didn't realize that. The sillycase challenge in the older retired Python Collections course had a comment that directly specified that
round
should be used so that is what I based my answer on. Thank you for the correction.Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsYes, the old one did specifically hint at the round() function which meant that the middle letter on odd length strings sometimes went with the upper half and sometimes with the lower half.
On the new one it looks like it should always be with the upper half.