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 trialElizabeth McInerney
3,175 PointsPython/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?
5 Answers
Chris Freeman
Treehouse Moderator 68,425 Pointsformatted 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
3,175 PointsThanks, 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
4,727 PointsElizabeth 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
66,191 PointsCan I get your code for the challenge?
Elizabeth McInerney
3,175 Pointsstring = "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
3,175 PointsI am sorry, when I transferred the code, the formatting did not keep. It runs when formatted correctly in my Workspace.
Devin Scheu
66,191 PointsYou 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
66,191 PointsAlso can I get the URL link to the challenge your on?
vdivekar
3,544 PointsI 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
Treehouse Moderator 68,425 PointsThe 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
3,175 PointsThanks. I did not realize you could do that! It seems that Python has lots of ways to minimize code.
Chris Freeman
Treehouse Moderator 68,425 PointsChris Freeman
Treehouse Moderator 68,425 PointsHave you tried Control-x for cut, Control-c for copy, Control-v for paste?