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
Bob Pickles
1,363 PointsPlease explain the differences.
On the python sillycase exercise, I toyed with it for a long time before I finally tried what I consider to be bad practice. To that end, I would like for you to explain to me why in your site, this code works:
def sillycase(s): stop=len(s)/2 first=s[:int(stop)].lower() second=s[int(stop):].upper() return first+second
but this fails with "Be sure to use integers for indexes. int() or // will be needed"
def sillycase(s): stop=int(len(s))/2 first=s[:stop].lower() second=s[stop:].upper() return first+second
Even though both pieces of code give you the same result when you create a python script and run it.
python working.py treeHOUSE
python nonworking.py treeHOUSE
1 Answer
andren
28,558 PointsThe second version of your code will only run in Python 2, if you run it in Python 3 (Which is what is taught at Treehouse) you will receive the following exception:
TypeError: slice indices must be integers or None or have an __index__ method
The reason for that is that one of the large things that was changed between Python 2 and Python 3 is the behavior of the division operator. In Python 2 the division operator would always result in an int number if the numbers being divided were ints. Which meant that operations like 3 / 2 would result in 1 rather than 1.5.
In Python 3 this behavior was changed so that the division operator always results in a float regardless of the numbers involved. But they also introduced a new division operator // which behaves in the same way the division opetator worked in Python 2.
In the first line of your second code example:
stop = int(len(s)) / 2
You turn the length of the string into an int and then divide it by 2. Since Python 3 always returns a float when dividing numbers you end up with a float being stored in stop, which is invalid as a step.
There are two efficient ways around this. The first is to perform the division inside the int method call like this:
stop = int(len(s) / 2)
That will cause Python to convert the float that the division results in into an int. The second way is to use the // operator like this:
stop = int(len(s)) // 2
Since that will result in the same behavior you are seeing in Python 2.
As an addendum the reason why your first code example works is that you convert the content of stop (which is a float) to int in the place it is being used, which results in the same effect as the two solutions I've written about above.