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 trialEugene Paitoo
3,922 Pointssquared
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))
2 Answers
Jennifer Nordell
Treehouse TeacherHi 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!
Eugene Paitoo
3,922 PointsThanks very much!