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 (2016, retired 2019) Slices sillyCase

ido sh
ido sh
3,926 Points

how i can use // or int()?

I need you to create a new function for me. This one will be named sillycase and it'll take a single string as an argument. sillycase should return the same string but the first half should be lowercased and the second half should be uppercased. 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

2 Answers

Steven Parker
Steven Parker
231,236 Points

Here's an example:

If you were to divide 3 by 2 ("3/2"), the result would be 1.5, but that's not a valid value to use as an index. So if you enclosed the division in the int function ("int(3/2)") you would get a result of 1. You could also get that result by using the special integer division operator ("3//2").

ido sh
ido sh
3,926 Points

Thank you, but I mean to ask how I can use that function in the specific exercise

Steven Parker
Steven Parker
231,236 Points

I figured that. :smile: Well, let me suggest that you might rethink your strategy and you may find a simpler way to achieve the objective. And this simpler way might involve dividing a value by 2 and then using it as an index, but only if the result is an integer.

Julie Maya
Julie Maya
14,666 Points

I used len() and // to solve this question.

def sillycase(n):
    return n[0:len(n)//2].lower()+n[len(n)//2:].upper()