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

Dan Garrison
Dan Garrison
22,457 Points

Sillycase Challenge

Can anyone tell me why my code is not working for this challenge? It works when I run it from the console on my computer. So I'm kind of at a loss as to why it's not working here.

I get an error message saying that slice indices must be integers or None or have an index method.

silly.py
# 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(text):
    middleIndex = int(round(len(text))) / 2
    firstHalf = text[:middleIndex].lower()
    secondHalf = text[middleIndex:].upper()
    combined = firstHalf + secondHalf
    return combined

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Well. middleIndex = int(round(len(text))) / 2 there's a problem in this line when you're calculating the middleindex. It should be

middleIndex = int(round(len(text)/2))
Dan Garrison
Dan Garrison
22,457 Points

Thanks. That did the trick. Any idea why it worked when I ran it from the console on my computer? I tried printing the value of middleIndex to check for the number it was outputting and it was the same regardless of where I divided by 2. Is the reader in Treehouse just looking for a specific syntax in this case?

William Li
William Li
Courses Plus Student 26,868 Points

try test with len(text) in odd number, and you'll see the difference.