Welcome to the Treehouse Community
Looking to learn something new?
Hello, i need help some with with my code. I'm a little bit confused here
def squared(item): try: item = int(5) return(5 ** 2) except ValueError: return (len(item))
Jennifer NordellTreehouse Teacher
Hi there! You are so close here! The problem really lies in that you are hard-coding the values. We want this to be very flexible and accept any input coming into the function. You can think of the parameter as a local variable definition. Whatever is sent to the function will be assigned the temporary variable name of
item. Also, in your last line you are returning the length of the item instead of the length of the item times the item. If I make your code a little more generic, it passes with flying colors! Take a look:
def squared(item): try: item = int(item) #if the item can be converted to an integer return item ** 2 #return the square of the integer except ValueError: #if it can't be converted to an integer return item * len(item) #return the item times its length
All in all, I'd say job well done. Just keep in mind that a parameter is a variable just like any other that you might have declared other than once the function ceases execution, it will no longer be available. This means that you cannot access the variable
item outside that function. This idea ties to
scope which I'm sure you will be learning plenty about going forward.
Hope this helps!
Thanks very much!