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 trialHunter Gass
4,878 Pointssquared objective
really struggling with this last objective. I don't know what im doing wrong any help would be much appreiated!
def squared(x):
try:
x = int(x)
except:
return len(x) ** 2
else:
return x * x
print squared(5)
print squared("3")
print squared("boo")
Hunter Gass
4,878 PointsI figured it out! thank you so much Steven!
1 Answer
Steven Parker
231,271 PointsYou have an issue with the value you return on an exception.
The instructions say to "return the argument multiplied by its length". But your statement is returning the square of the length instead. You probably want something more like this:
return len(x) * x
Hunter Gass
4,878 Pointsits not working
Steven Parker
231,271 PointsWell, you also need to get rid of those print statements at the bottom — they are not part of the challenge.
Hunter Gass
4,878 Pointsi keep getting syntax error
Steven Parker
231,271 PointsDid you change more than just the one line I showed you (plus removing the *print*s)?
Perhaps you should show your entire code as it is now.
Hunter Gass
4,878 Pointsdef squared(x):
try:
x = int(x)
except:
return len(x) ** 2
else:
return len(x) * x
I really can't thank you enough for your persistence to help me lawl
Steven Parker
231,271 PointsIt looks like you replaced the wrong return statement. My suggested replacement was for what you return on an exception, where you currently return the square of the length.
But you put it on the line where you don't have an exception instead (that line was fine as it was).
Hunter Gass
4,878 PointsHunter Gass
4,878 Pointsso then my code here is okay?