Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Slices sillyCase

For example, with the string "Treehouse", sillycase would return "treeHOUSE".

For example, with the string "Treehouse", sillycase would return "treeHOUSE". Don't worry about rounding your halves, but remember that indexes should be integers. You'll want to use the int() function or integer division, //.

sillycase.py
def sillycase(i):
    half = len(i)//2
    for x in i:
        o = str(i[x]).lower()
        i[x] = o

        count = 0
        count += 1
        if count <= half:
            break

    for half in i:

        o = str(i[x]).lower()
        i[x] = o

    return i

4 Answers

Steven Parker
Steven Parker
221,070 Points

You're working way to hard.

This challenge is intended to give you practice with slices, even though you can pass other ways. But if you use them, you can solve this in just a few lines (and no loops).

Steven Parker
Steven Parker
221,070 Points

So for example, let's say you wanted just the first half of a string named "i". You've already computed "half" the length, so now you could apply a slice: "i[:half]". Then if you wanted, you could apply a case-changing function to that. You could also concatenate that whole thing to another slice (perhaps with the arguments arranged to return a different part of the string) with another case-changing function.

Then if you were to return that entire expression, you'd have everything you need for the function to do in just two lines of code. :smirk:

It's a learning process steven. If you could pull me back a little and enlighten me would be very nice of you coz I seem to be a little lost here.

Steven Parker
Steven Parker
221,070 Points

I added some extra hints in a comment on my previous answer ... see if those help.

Steven Parker
Steven Parker
221,070 Points

I"m not sure what you mean by this. The shorter solution doesn't involve "chaining" anything.

Jonathan Prada
Jonathan Prada
7,039 Points

def sillycase(s): half = len(s)//2 return "{}{}".format(s[:half].lower(), s[half:].upper())

Steven Parker
Steven Parker
221,070 Points

That's one way to do it. But you can make it more concise by not using the format.