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 (Retired) Slices sillyCase

sillycase

where did i go wrong

silly.py
# 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"
def sillycase(my_string):

  for letter in my_string:
    if index < round(len(my_string)/2):
      my_string = letter.lower()

    else:
      my_string = letter.capitalize()

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

there're some issues with your codes.

  1. no return value
  2. index variable is not declared.
  3. in each run of the the loop, you keep setting my_string to new value, so let's assume your for loop works as advertised, after the loop, my_string will only end up as a single letter.

One way to approach this problem is to slice the string in half, downcase the first half, then upcase the 2nd half, and combine the two back to one string.

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

thanks! for the fun of it, is it possible to use a loop to step into like i tried?

William Li
William Li
Courses Plus Student 26,868 Points

Sure, it'd however make the code quite a bit more complex, but for educational purpose, it's good to have curiosity about different way of solving a problem.

def sillycase(string):
  result = ""
  midpoint = round(len(string)/2)
  for index in range(len(string)):
    if index < midpoint:
      result += string[index].lower()
    else:
      result += string[index].upper()
  return result

You see, it makes the code longer and a bit harder to read.

Great!!