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 trialJoeseph Wolfe
Courses Plus Student 1,010 PointsNot sure why this is not working for me, this works in my interpreter as desired
def squared(value):
if isinstance(value, int) == True:
return int(value) ** 2
else:
return value * len(value)
This code seems to work as desired for me but the quiz is returning "Bummer 'squared' didn't return the right answer.' When it has.
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(value):
if isinstance(value, int) == True:
return int(value) ** 2
else:
return value * len(value)
2 Answers
Steven Parker
231,269 PointsThe challenge says that the function should return the argument squared if it can be converted into an integer.
But your function is only doing that if the argument already is an integer. You may have overlooked the distinction if you only tested it with integers and non-numeric strings.
You just need to replace your if isinstance...else with try...except.
Joeseph Wolfe
Courses Plus Student 1,010 PointsThanks Steven, I figured they wanted it done a certain way so I re-read again and used a try/except and I got a correct answer.