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

Please help with Python Code

Here is the challenge: Create a function named sillycase that takes a string and returns that string with the first half lowercased and the last half uppercased. E.g. 'Treehouse' should become 'treehOUSE'

Is it possible someone can give me a step by step breakdown?

I'm so confused. I only have:

def sillycase(string): 
  return string

Thanks

4 Answers

Declare the method like you have already:

  def sillycase(string)

Split the string into the first and second halves:

    firstHalf = string[0:len(string) / 2]
    secondHalf = string[len(string) / 2:]

Make the first half lowercase and the second half uppercase:

    firstHalf = firstHalf.lower()
    secondHalf = secondHalf.upper()

Finally return the result by combining both halves:

  return firstHalf + secondHalf

Putting it all together and shortening it a bit you end up with the following:

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

Thanks very much. I find that from the videos and studying I have the pieces to the puzzle but when i'm asked to put them all together it becomes really difficult for me.

You'll get there Tony Brackins! It really is a bit like solving a puzzle; you know where it starts (def sillycase) and where it should end (the requirement of the code challenge) and it's just a matter of getting those intermediate steps worked out. Cheers!

This challenge has a few parts:

Part one Split the string in half: You can use string indexing to do this. Example:

firstHalf = myString[:len(myString)/2] #this will give you the first half I am sure you can figure out the second half

Then you will have to make the first half lowercased and the second half uppercase. There are String Methods you can use to do this. You can learn about those here.

https://docs.python.org/3/library/stdtypes.html#string-methods

Lastly you will need to put the halfs back together with the plus sign:

newString = firstHalf + secondHalf
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Since we use Python 3, probably not a great idea to link to Python 2 docs.

Sorry I use python 2 and often forget. Thank you for correcting me I have edited my response accordingly.

Hey guys,

still getting an error: Bummer! slice indices must be integers or None or have an index method

I probably should have included these comments:

# 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"

Here's my code so far:

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

I also tried making a variable string = "Treehouse"

Thanks in advance!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Division in Python 3 always creates a float. That's why you have to use round(), which your code doesn't.

Oversite on my part Tony Brackins, i'm still using 2.7 locally. D'oh!

Thanks for the catch Kenneth Love

Ok that worked! thx!