Welcome to the Treehouse Community
Looking to learn something new?
Paul Heneghan14,337 Points
Tried this as a if/else, and it works, but I'm missing something when I use try/except.
# EXAMPLES # squared(5) would return 25 # squared("2") would return 4 # squared("tim") would return "timtimtim" def squared(num): if type(num) != int: num = int(num) try: return(num **2) except: return(num * len(num))
Jennifer NordellTreehouse Teacher
Hi there! You're so close here! And because you're so close, I'm going to see if you can get the solution with a few hints. First, you can remove the
if part altogether. All you'll need here is a try and except. And what exactly are we trying? We're trying to convert the input into an int. You need to be testing first if the number can be turned into an int and then squared and return that result. However, if it fails, it should go to the
except ValueError block, where your code is currently correct Hope this helps!