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 trialSean Hetzel
1,334 PointsTested in python ide with correct output for all cases such as 2, "2", and "two."
treehouse says "squared didn't return the right answer. I got the right answer for all other cases in IDLE. I don't understand why its not producing the correct output.
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(arg):
try:
return print(int(arg) ** 2)
except:
return print(arg * len(arg))
1 Answer
Michael Hulet
47,913 PointsThe problem here is that you're wrapping the return
values in print
statements. You're print
ing out the correct answer, but the print
function itself always return
s None
, so your function will only ever return None
. If you remove the print
statements, I think it'll work fine. That being said, it's considered bad form in Python to just generally except
everything. In your case, I think you want to except ValueError
in case the input can't be converted to an int