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 trialFHATUWANI Dondry MUVHANGO
17,796 Pointsproblems with stringcases
i dont know how to reverse a string. and i think this code is missing something because it keeps saying "try again"
# Handy functions:
# .upper() - uppercases a string
# .lower() - lowercases a string
# .title() - titlecases a string
# There is no function to reverse a string.
# Maybe you can do it with a slice?
def stringcases():
string_case = "hi how are you"
x = string_case.upper()
y = string_case.lower()
z = string_case.title()
u = string_case[:-1]
return x, y, z, u
8 Answers
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsDid you try the function I posted yesterday? I just copied and pasted it into the code challenge below the comments and it passed. Here it is again:
def stringcases(string):
x = string .upper()
y = string .lower()
z = string .title()
u = string [::-1]
return x, y, z, u
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHey Fhatuwani,
You were close, you don't need to declare a string_case variable, because the function is going to be passed a string. So, you need to add a parameter inside the parentheses after the function name. That parameter then becomes the object that the .upper(), .lower(), and .title() methods are performed on - as well as the slice ([::-1]).
You're function should look like this:
def stringcases(string):
x = string .upper()
y = string .lower()
z = string .title()
u = string [::-1]
return x, y, z, u
I hope that helps! Let me know if you have any more questions.
Script Code
6,631 PointsTry u=string_case[::-1] remember that slice operations have [i:j:k] whereas k is an optional stride (a.k.a step). If left out it is uses its default value 1. In your script you defined i and j and left out k.
Try to play with the stride value. [1,2,3,4,5][::-2] This would return [5,3,1]
[1,2,3,4,5][4::-2] This would also return [5,3,1]
FHATUWANI Dondry MUVHANGO
17,796 Pointshi, i tried running it with [::-1], but it keeps telling me to "try again." i think there is a problem with my def or return
Script Code
6,631 PointsWhat is your exact assignment?
Because as it is right now it is valid code. This is what your function returns. a = stringcases()
print(a) ('HI HOW ARE YOU', 'hi how are you', 'Hi How Are You', 'uoy era woh ih')
Jason Anello
Courses Plus Student 94,610 PointsThe challenge is linked to at the top right corner in case you need to review.
Script Code
6,631 PointsThanks Jason!
FHATUWANI Dondry MUVHANGO
17,796 Pointsi also dont know this is what i did
def stringcases(): string_case = "hi how are you" x = string_case.upper() y = string_case.lower() z = string_case.title() u = string_case[::-1] return (x, y, z, u)
FHATUWANI Dondry MUVHANGO
17,796 Pointsthis is frustrating, coz my script is right but i cant get through, it keeps on saying "try again" and ive tried everything i know to try. its impossible
Jason Anello
Courses Plus Student 94,610 PointsDid you take a look at Chris Jones answer?
There were 2 things that you needed to fix.
FHATUWANI Dondry MUVHANGO
17,796 Pointsyhooo thanks guys, didnt know that a mistake so simple could hold me up for so long
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsNo problem! We've all been there...sometimes we just get stuck in a certain way of thinking about it:).