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

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
216,136 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
216,136 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()