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

Luke Weber
Luke Weber
3,897 Points

Error Message: slice indices must be integers or None or have an __index__ method

# The first half of the string, rounded with round(), should be lowercased.
# The second half should be uppercased.
# E.g. "Treehouse" should come back as "treehOUSE"

def sillycase(string): 
  firsthalf = string[:(len(string)/2)].lower()
  secondhalf = string[(len(string)/2):].upper()
  whole = firsthalf + secondhalf
  return whole 

I tested this function in my terminal and it ran fine, but the challenge task gives me an error that says my slice indices are not integers? Can someone give me feedback on why I am receiving this error?

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You don't seem to be using round() like the hint suggests. So a word like "hello", that has 5 characters, would be trying to go from, say 0 to 2.5, which isn't an integer.

Luke Weber
Luke Weber
3,897 Points

Hi Kenneth! Thanks for your response.

I'm confused how you are supposed to use the round() function within a string slice. I thought string indices were always integers (and can't be floats), so it would round by default?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Nope. Python does its best to never do things for you (i.e. doing things implicitly) so if you do "Hello[2.5]", Python will throw an exception because you gave it a non-integer index. That's why you have to use the round() to make sure it's an integer. Ultimately, you're responsible for making sure you have the correct data types.

Luke Weber
Luke Weber
3,897 Points

Oh okay, got it! Thanks.