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
brian albano
4,316 PointsWould someone help me break down and learn this "Sillycase" function Python exercise
Hi
Got stuck on this exercise today and found the answer so I could move on, but I'm writing this post to be able to go back and properly understand this function so i can sleep at night
Now the exercise was this "Create a function named sillycase that takes a string and returns that string with the first half lowercased and the last half uppercased."
I don't even remember round() being covered, but I guess the course would taken forever if they taught everything and if I used something like help or dir I suppose I would find round().
Anyway, the #s notes in the exercise said # The first half of the string, rounded with round(), should be lowercased.
The second half should be uppercased.
E.g. "Treehouse" should come back as "treeHOUSE"
and I found someone who gave the answer of
def sillycase(word):
x = round(len(word)/2)
y = word[x:].upper()
z = word[:x].lower()
return z+y
and that worked...
Could someone go through the function for me?
To be honest I actually see how the function works, my main problem was I wasn't able to run the problem to get treeHOUSE
If I ran the function, how do I get it to work = treeHOUSE
It's not print (sillycase(treehouse))
I'm sure this is a stupid question, but I've been doing really great so far yet I think I could be a bit burned out and missing a really easy solution to this question
so basically how do I run this function and get the desired result output of 'treeHOUSE" INPUT/OUTPUT WISE
2 Answers
Jeffrey Ruder
6,408 PointsOkay, I'm going to try to break down the code you posted.
First, it takes the length of word, divides the length by 2, and rounds the result. It assigns the rounded result to variable x.
The function will use x as a halfway point in the middle of the word.
Next, it takes the second half of the word (from x to the end) and uses the .upper method to make it uppercase. It assigns that to variable y.
Next, it takes the first half of the word (from the beginning to x) and uses the .lower method to make it lowercase. It assigns that to variable z.
Then the function returns z + y which makes the new word.
To answer your other question, I think that:
print(sillycase("Treehouse"))
should work.
Are you leaving out the quotation marks? That would cause an error.
Benedictt Vasquez
2,130 PointsThis work on console of workspace but not in the challenge. I don't know why.:
def sillycase(str):
... x = str
... y = round(len(x)/2)
... z = (len(x)-y)
... minus = x[:y].lower()
... mayus = x[-z:].upper()
... print (minus+mayus)
...
sillycase('Treehouse')
treeHOUSE
sillycase('kenneth')kennETH
I can't pass the challenge. If you get the correct answer please let us know.
brian albano
4,316 Pointsbrian albano
4,316 PointsThank you, this was helpful indeed. A few days back I think I was spinning from everything lessons wise, but feeling far less dizzy lately, but this walk through really did help make things even clearer.