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 trialCam Treschuk
7,378 PointsCode problem
This challenge isn't working. Am I doing anything wrong?
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
try:
int(num)
return num * num
except TypeError:
str(num)
return num * len(num)
2 Answers
asd asdf
Courses Plus Student 298 Pointswhen you do int(num)
, that statement doesn't change the value of num
, you need to assign it, like num = int(num)
. If you add that it should work. Same for str(num)
but that line isn't needed anyway.
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHey Cam,
I think the int()
function will throw a ValueError
, not TypeError
, when the argument can't be cast to an int
. Does changing the except
clause to ValueError
instead of TypeError
pass the challenge?
Below is what I see when I use the python shell. Notice that a ValueError
is thrown.
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> num = "tim"
>>> int(num)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
int(num)
ValueError: invalid literal for int() with base 10: 'tim'
>>>
Let me know if that helps!
Cam Treschuk
7,378 PointsCam Treschuk
7,378 PointsYep, that worked. Thanks!