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 trialMichael Randall
Courses Plus Student 10,643 PointsNeed 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.
# 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
Treehouse Moderator 68,441 PointsTwo 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, useround()
: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.
Andrea Rivera Figueroa
8,314 PointsI 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() )
Michael Randall
Courses Plus Student 10,643 PointsMichael Randall
Courses Plus Student 10,643 PointsThanks! That helped without completely giving away the answer.