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

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Python/Collections/slices/sillycase

My code works perfectly in workspaces, but when I try to execute it in the course, I get an error that says the code returns None instead of kennETH. Apparently they are using the string Kenneth to test people's code. But when I use that string in my workspace, I get the correct answer with my code. Since I can't cut and paste, I have retyped it twice, and it still will not run. Any ideas?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Have you tried Control-x for cut, Control-c for copy, Control-v for paste?

5 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

formatted using the triple-backtick-python (as mentioned in the Markdown Cheatsheet):

string = "Kenneth"

def sillycase(string):
    stringU = string.upper()
    stringL = string.lower()
    halfway = round(len(string)/2)
    stringF = stringL[0:halfway] + stringU[halfway:len(string)]
    print(stringF)

sillycase(string)

Based on this code, the issue is that you need to return a value. While the print shows you have the correct value, nothing is returned. Change the last line to:

    return stringF
Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Thanks, adding the return did the trick. I guess my code in Workspaces did not care if the data was returned, so it ran just fine. Thanks also for directing me to the Markdown Cheatsheet. I am unfamiliar with that and will look for it.

Michael Pastran
Michael Pastran
4,727 Points

Elizabeth this is the way i did it just now.

def sillycase(str):
    halfway = round(len(str)/2)
    return str[:halfway].lower() + str[-halfway + 1:].upper()

i also found out that you could just do return str[:halfway].lower() + str[halfway:].upper()

Devin Scheu
Devin Scheu
66,191 Points

Can I get your code for the challenge?

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

string = "Kenneth"

def sillycase(string): stringU = string.upper() stringL = string.lower() halfway = round(len(string)/2) stringF = stringL[0:halfway] + stringU[halfway:len(string)] print(stringF)

sillycase(string)

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

I am sorry, when I transferred the code, the formatting did not keep. It runs when formatted correctly in my Workspace.

Devin Scheu
Devin Scheu
66,191 Points

You can format it correctly by typing three back ticks in a row which are above your tab key, then the name of the language, your code, and three back ticks to close it, like this:

//```JavaScript
//Code Goes Here
//```
Devin Scheu
Devin Scheu
66,191 Points

Also can I get the URL link to the challenge your on?

vdivekar
vdivekar
3,544 Points

I tried copy+pasting your code that you posted here and formatted it. I don't know what's wrong with it, but can you try calling the string you'll pass in by a different name? i.e.

string = "Kenneth" 
def sillycase(arg_string): 
# call the string you work with *inside* the function "arg_string"

sillycase(string)

I also have trouble with copy and pasting with the code challenges (yes I tried Ctrl+C/X), but I find that if I select everything and drag it into a text editor window (or whereever) with my mouse, the code does transfer over. I don't have any problems pasting into the code challenge area though. For that, it's just Ctrl+V.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The names do not have to match on the call. In the call sillycase(string), string is the local value of the variable "string". In the scope of the defined function def sillycase(arg_string), arg_string is local to the function and gets set to the first positional argument passed. In this case it's the value of string.

Even if they do match, the scope of the variables are different and the string inside the defined function is made local.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Thanks. I did not realize you could do that! It seems that Python has lots of ways to minimize code.