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

My solution works in the Python shell but not here.

Hi all,

I tried my solution locally in the python shell and it works just fine. But with the Treehouse interpreter I get the following error message:

"Bummer! slice indices must be integers or None or have an_index_method"

Has it maybe something to do with the Python version?

Thanks!

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(s):
    string_as_list = list(s)

    first_half = string_as_list[:len(string_as_list) / 2]
    second_half = string_as_list[len(string_as_list) / 2:]

    first_half = ''.join(first_half).lower()
    second_half = ''.join(second_half).upper()

    return first_half +  second_half

# print(sillycase('Treehouse'))
# >>> treeHOUSE

2 Answers

Hi Benjamin, I think part of the issue is that you need to use round(). Also, you do not need to create a variable to turn your string into a list. I edited your code a little and was able to get it to pass like this:

def sillycase(s):

    first_half = s[:round(len(s) / 2)]
    second_half = s[round(len(s) / 2):]

    return first_half.lower() + second_half.upper()

sillycase("Treehouse")

If you want to condense it even more, you could do it in one line, like so (this is the code I used in my challenge, and I am using a more descriptive variable name for the string so it's a little easier for me to read:

def sillycase(a_string):
  return a_string[:round(len(a_string) / 2.0)].lower() + a_string[round(len(a_string) / 2.0):].upper()

Thank you! It is working fine now. It was because I needed to round after the division. Like Kenneth pointed out, it is different now in Python 3.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I'm guessing your Python is 2.something, huh? In Python 2, stupidly, dividing with an integer, as you're doing, always gives you an integer. So 5 / 2, instead of the correct 2.5, will give you 2. Since we use Python 3 here at Treehouse, you'll have to use round(), like Tree Casiano pointed out.

Yes you are right. I have installed Python 2. something on my computer. But this is something quite important to know and it solved my problem. Thanks you!