Welcome to the Treehouse Community
Looking to learn something new?
Faisal RanaPython Web Development Techdegree Student 2,008 Points
What's wrong with my code ? When I run it in IDLE it shows " AttributeError: 'tuple' object has no attribute 'upper' "
Here is the Challenge: Create a function named stringcases that takes a single string but returns a tuple of different string formats. The formats should be:
All uppercase All lowercase Titlecased (first letter of each word is capitalized) Reversed There are str methods for all but the last one.
def stringcases(*string): return string.upper() return string.lower() return string.title() return string.reverse() stringcases("syntax", "functions", "methods", "tuples", "dict")
Hey, in this challenge they would pass you a single string such as
"functions" - as it's not multiple arguments (a list), there is no need to unpack the string variable. It would result in an error anyways as you cannot call .lower() or any of the other functions on a tuple or list.
- Regarding the last return statement, it would also raise an Error as .reverse() does not exist, there are many ways to reverse a string but by far the easiest one, in my opinion, is to just use
string[::-1]- it returns all characters of the string backwards
- The last thing would be returning a tuple of the answers - just put them in a single return statement and separate them with commas (you could add brackets if it makes it easier to understand)
def stringcases(string): return ( string.upper(), string.lower(), string.title(), string[::-1] )