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 trialAlexander Davison
65,469 PointsSquared function
It says "Bummer! 'squared' didn't return the correct result"
What's wrong???
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(arg):
try:
if int(arg) == arg:
return arg ** 2
except:
try:
return int(arg) ** 2
except:
return arg * len(arg)
1 Answer
Jennifer Nordell
Treehouse TeacherOk you're checking in your code to see if the int version of the argument is equal to the argument. If it is, return the square of it. Then if it isn't possible to make it an int... you check it again to see if it can be made an int and returned as a square directly. When that fails then you send back the string printed out a certain number of times. But you don't say what kind of exception you're looking to "catch".
Somewhere in all that you managed to overthink it a bit, I think. Here's the code I used. I hope it helps!
def squared(arg):
try:
return int(arg) ** 2
except ValueError:
return arg * len(arg)
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsThanks you so mush! :D