Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App Squared


Hello, i need help some with with my code. I'm a little bit confused here

def squared(item):
        item = int(5)
        return(5 ** 2)
    except ValueError:
        return (len(item))

2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse 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):
        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! :sparkles:

Thanks very much!