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

victor E
victor E
19,146 Points

this one was a really tough one for me

not sure how to approach this one to be honest

sillycase.py
def sillycase(word):
    half = (int.len(word))/2
    word = upper.word[-1:-{}].format(half)
    return(word)
Kent Åsvang
Kent Åsvang
18,823 Points

If you could paste in more information about the challenge I could try and help out.

3 Answers

This one is a tough one indeed! The most effective way to solve a problem is to split it up into easy problems, and solve each one of those mini-problems. Perhaps this problem can be split up into five steps:

  • Find the index of the midpoint
  • Select the first half of the string
  • Select the second half
  • Convert first half to lowercase
  • Convert second half to uppercase

Each one of these problems are pretty easy.

def sillycase(string):
    midpoint = len(string) // 2  # Step 1
    first_half = string[:midpoint]  # Step 2
    second_half = string[midpoint:]  # Step 3
    lowercase = first_half.lower()  # Step 4
    uppercase = second_half.upper()  # Step 5
    return lowercase + uppercase

Once you've got this, you can refactor (simplify) the code a little bit:

def sillycase(string):
    midpoint = len(string) // 2
    first_half = string[:midpoint].lower()
    second_half = string[midpoint:].upper()
    return first_half + second_half

The above code is the cleanest code I can think of, and I personally would solve the challenge with the code above, but if you are going for minimal lines (and minimum clarity), this snippet is the winner:

def sillycase(string):
    return string[:(len(string)//2)].lower() + \
           string[(len(string)//2):].upper()

I hope this helps! ~Alex

Kristian Vrgoc
Kristian Vrgoc
3,046 Points

Hey, why are you using the \ after the + in the last code ? Thanks. Kris

Hello Kristian,

The \ tells Python that the next line is actually part of the current line.

This would cause an error:

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

But this will work:

def sillycase(string):
    return string[:(len(string)//2)].lower() + \
           string[(len(string)//2):].upper()

You may say, "But wait! Python usually figures out if the line is complete or not, and it will automatically do this for me!". You're right; Python is clever. In certain cases, like this...

print(
  "Hello world!"
)

Python appropriately prints "Hello world!" as you might expect. But in the case of something like this:

1 +
2

Python freaks and throws a SyntaxError. In this case you might want to write

1 + \
2

# Or, even better:

1 + 2

I hope this helps!

Generally, you should never use \. Try to put all of the code on the same line. If the code looks too confusing (or if the line exceeds ~79 characters), split the line into multiple lines of code.

victor E
victor E
19,146 Points

Thank you I appreciate this!

You're very welcome :)

Kristian Vrgoc
Kristian Vrgoc
3,046 Points

@ Alexander Davison Thanks you very much. Kris

No problem. :+1: