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

Duarte Reis
seal-mask
.a{fill-rule:evenodd;}techdegree
Duarte Reis
Python Web Development Techdegree Student 897 Points

Got it right, not sure about the code

Hi, the code I came up to solve the challenge was this one:

def sillycase (string):
    half = int(len(string)/2+1)
    half1 = string[:half].lower()
    half2= string [half:].upper()
    return half1+half2

However, I was only able to get to this code by reparing 2 mystakes:

1 : first, I had defined half as:

 half = len(string)/2

I got and error saying that index must be an integer. Since the len() function returns a number, isn't it safe to assume it will be an integer? Or is it because the half can be a double?

2: I had half1 defined as:

half1 = string[:half+1].lower()

It didn't get the result it was supposed to. It was like it wasn't considering the addition I was making to the stop index. I was able to solve it by adding it to the half variable. What is wrong here

Is my code clean or could I do something different to get the result?

Hope you can help.

Thanks in advance.

Duarte

2 Answers

Hi Duarte

You can simplify your code a lot more. You basically want to get a rounded down integer representing the half way point of the string. See below how i did it.

def sillycase(mystring):
    half = round(len(mystring)/2)
    return mystring[:half].lower()+mystring[half:].upper()
Duarte Reis
seal-mask
.a{fill-rule:evenodd;}techdegree
Duarte Reis
Python Web Development Techdegree Student 897 Points

Hi Andreas, thanks a lot for your reply!

So, I understand that if you are making that round() the problem with my "len(string)/2" was that it was a double?

I have 2 questions about your code:

1 - I understand a little of VBA and I'm used to declare everything I make. Every step I take. Because it is a definition and I will just use what it returns I don't need to do it and I can simplify? When I'm writting other code is it still fine to simplify this way?

2 - what was the problem with the my initial variable:

half1 = string[:half+1].lower()

Thanks a lot!

Duarte

Hi Duarte

Yes len(string)/2 return a double. When you try to slice it expects an integer and not a double.

Languages like VBA are all strongly typed languages, meaning you need to tell the compiler what the variable is in advance for ex String a = "Hello". Python on the other hand is a loosely typed language, so you can say a= "hello" or a=22 both are valid syntax.

half1 = string[:half+1].lower()# half1=treeh and not tree