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

Sergei Miroshnikov
Sergei Miroshnikov
3,313 Points

Confusing error message , function is working as requested .

I have also added print (type (x)) to verify that that x is integer , however I am getting an error Bummer! slice indices must be integers or None or have an index method"

# 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(justastring = 'Treehouse'):
    x = len(justastring) / 2
    print (type(x))
    return (justastring[:x].lower() + justastring[x :].upper())

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Your code is very close. The issue is when there is an odd number of characters, your slice variable x is a float. Correct this by using the round() function to get the nearest integer:

x = round(len(justastring) / 2)
Sergei Miroshnikov
Sergei Miroshnikov
3,313 Points

actually when I use round (x) i get the following error from python interpreter "TypeError: slice indices must be integers or None or have an index method" and its type changes to float . I am python 2.7.10. I don't mind casting to int , but it is pointless, hence its integer from the start

x = int(round(len(justastring) / 2))

anyway for some reason the line I just typed above worked , would love to hear explanation why .

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The challenges run in Python 3, but I see your error when running Python 2:

$ python2
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def sillycase(justastring = 'Treehouse'):
...     x = len(justastring) / 2
...     print (type(x))
...     return (justastring[:x].lower() + justastring[x :].upper())
... 
>>> sillycase()
<type 'int'>
'treeHOUSE'
>>> def sillycase2(justastring = 'Treehouse'):
...     x = round(len(justastring) / 2)
...     print (type(x))
...     return (justastring[:x].lower() + justastring[x :].upper())
... 
>>> sillycase2()
<type 'float'>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in sillycase2
TypeError: slice indices must be integers or None or have an __index__ method
>>> round(len('Treehouse'))
9.0
>>> quit()

$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def sillycase(justastring = 'Treehouse'):
...     x = len(justastring) / 2
...     print (type(x))
...     return (justastring[:x].lower() + justastring[x :].upper())
... 
>>> sillycase()
<class 'float'>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in sillycase
TypeError: slice indices must be integers or None or have an __index__ method

>>> def sillycase3(justastring = 'Treehouse'):
...     x = round(len(justastring) / 2)
...     print (type(x))
...     return (justastring[:x].lower() + justastring[x :].upper())
... 
>>> sillycase3()
<class 'int'>
'treeHOUSE'

# Python 3, round() to the default zero places returns an int instead of a float
>>> round(2.45)
2
>>> round(2.45, 1)
2.5
>>> type(round(2.45))
<class 'int'>
>>> type(round(2.45, 1))
<class 'float'>

To bulletproof the code for both Python 2 and 3 add an extra cast to int

def sillycase(justastring = 'Treehouse'):
    x = int(round(len(justastring) / 2 ))
    print (type(x))
    return (justastring[:x].lower() + justastring[x :].upper())
``
James N
James N
17,864 Points

i don't think you can put variables in slices. put the len method directly in there.

Sergei Miroshnikov
Sergei Miroshnikov
3,313 Points

Thank you ! I will change my project interpreter to python 3 , I was suspecting compatibility issue , now thanks to you I know how to fix this , thank you very much !