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 trialannikaheflin
577 PointsI still don't get why this isn't working
Please help I so comfused
def squared(num):
if num == int:
return int(num) ** 2
else:
return(num * num)
except ValueError:
return(num) * len(num)
1 Answer
Bryan Reed
11,747 PointsInstead of using an If and Else just use Try and Except. Part of why what you have isn't working is because you're using an Except without a Try
def squared(num):
try:
return int(num) ** 2 # OR int(num) * int(num). Either will give the SAME answer he's just giving you a choice
# if the above return throws a ValueError because the num can't be converted into an integer
except ValueError:
return(num) * len(num)
# then this except will run instead
Try and forget about using int(num) ** 2 or num * num because both of these operations will return the same answer