Andrew BickhamPro Student 1,354 Points
its keeps giving me a bummer message saying I need to use int() or // which I have
def sillycase(word): first_half = int(len(word))/ 2 first_slice = word[:first_half].lower() second_half = int(len(word)) - int(first_half) second_slice = word[second_half:].upper() return int(first_slice) + int(second_slice)
Alex KoumparosPython Web Development Treehouse Moderator 32,453 Points
first_half = int(len(word))/ 2
word, which is a string, then computes the length of it, which is an int. Then you use the
int() function to turn that into an int (which it already is). Then you divide your int by 2, which turns it into a float.
You can prove that this is true in the Python console:
>>> word = 'hello!' >>> type(word) <class 'str'> >>> length = len(word) >>> type(length) <class 'int'> >>> i_length = int(length) >>> type(i_length) <class 'int'> >>> first_half = i_length / 2 >>> type(first_half) <class 'float'>
You can solve this by converting your result of the division by 2 to an int, instead of the length (i.e., put the whole calculation inside your
int() call). Alternatively, instead of doing any forced type conversion, you could use the integer division operator
// instead of the regular division operator
/, in which case you'll never a get a conversion to float here.
You have a redundancy in this line:
second_half = int(len(word)) - int(first_half)
The first half of this code is redundant: it turns something that is already an int (
len can only return an int) into an int. The second half of the code turns your float from before back into an int. However, it is too late to do the conversion here: your code will already have crashed with a TypeError in the previous line we looked at. Once you fix the error in that line,
first_half will be an int and so it will be redundant to use the
int() function here.
Moreover, this whole section of code is conceptually redundant. When you determine the index of the halfway point for your string, you have enough to make the second slice. For example:
>>> my_word = 'Python' >>> my_word[:3] # this will give me all the characters before index 3 'Pyt' >>> my_word[3:] # this will give me all the characters from index 3 'hon'
Observe that we use the same index value (here 3) to access both the first half and the second half.
return int(first_slice) + int(second_slice)
This code is attempting to convert the lowercase string of the first slice and the uppercase string of the second string of the second slice into ints (which will crash with a ValueError) and then add those ints together.
Remember that you want to return a string, which you just concatenate with
+ without turning them into numbers:
>>> concatenated = 'hello ' + 'world' >>> print(concatenated) hello world
Hope that clears everything up for you.
Andrei Oprescu9,546 Points
I have a noticed your mistake.
When you declare first_half and second_half, you only include len(word), but you also need to include / 2.
for clarification, you need to make the lines like this:
first_half = int(len(word)/ 2)
and also exclude the second_half variable because you will get the same numbers from first_half and second_half.
also, when you return, you shouldn't use int() because you are concatenating two strings.
hope this helps! if you need more help, don't hesitate to ask.