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 trialianmckellar
4,344 PointsProblems on code challenge: Squared.
Hey everyone!
So I've been fanageling my code since last night and for about an hour today with no luck. After reading other peoples problems on the forums and still coming to no conclusions I figured I'd ask for help :D
So, what am I doing wrong?
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
# remember to use try
# you also might want to use except
def squared(num):
try:
return num * num
if ValueError:
return num * int(num)
except TypeError:
return num * len(num)
2 Answers
jcorum
71,830 PointsIan, you need to try the conversion to an int, and return the square only if that works.
def squared(num):
try:
num = int(num)
return num * num
except:
return num * len(num)
P.S., also note that except should line up with try
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsHey Ian,
you have to try to convert the argument to an integer using "int(num)" before doing anything else. Also "if ValueError:" has to be "except ValueError". You also want to use "return num * len(num)" when catching the ValueError exception.
Regards, Florian
Levis Vazquez
961 PointsLevis Vazquez
961 Pointsthanks