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

Michael Randall
PLUS
Michael Randall
Courses Plus Student 10,643 Points

Need help with error message: Bummer! slice indices must be integers or None or have an __index__ method

I made sure that all of my slice indices are integer, so maybe I am misunderstanding this question. My code is attached.

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 "treeHOUSEdef sillycase(input_string):
def sillycase(input_string):
    operation_string = list(input_string)
    first = len(operation_string)/2
    #print type(first)
    first_part_string = operation_string[:first]
    last_part_string = operation_string[first:]

    for item in first_part_string:
        f_item_idx = first_part_string.index(item)
        #print type(f_item_idx)
        first_part_string[f_item_idx] = item.lower()

    for item in last_part_string:
        l_item_idx = last_part_string.index(item)
        #print type(l_item_idx)
        last_part_string[l_item_idx] = item.upper()
    input_string = ' '.join(first_part_string + last_part_string)
    #print type(input_string)
    return input_string

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Two fixes and a comment:

  • Your error is due to first not being an integer. It will be a float if there are an odd number of characters. As the hints suggest, use round(): first = round(len(operation_string)/2)
  • Your join() is including a blank space between the characters. Use ''.join() instead
  • Comment, you do not have to translate the input string into a list of characters. Indexing, .lower(), .upper() will work on whole strings. So you can divide input_string, change case on the parts, rejoin, then return result. Try to solve again without converting to lists.
Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

Thanks! That helped without completely giving away the answer.

I had the same error, so thanks, Chris, for suggesting the round function. I'll post my solution in case anyone is having trouble/wants to see a little more streamlined of a solution (i figured making it one line would be a little annoying to look at).

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