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 trialGreg Isaacson
Courses Plus Student 702 PointsA little bit stuck...
Hi All!
Im having a little bit of trouble on this question.
it keeps saying name error - test.
I want to make num = test so that I can turn test into an interger??
However it keeps saying name test undefined...have I not defined it???
Thanks!
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
num = test
try:
num = int(test)
return (test ** 2)
except TypeError:
return (len(test) * num)
3 Answers
Greg Isaacson
Courses Plus Student 702 PointsI got it working by REMOVING TYPE ERROR.
Why did TypeError make it not work???
Jason Anello
Courses Plus Student 94,610 PointsHi Greg,
I moved this to the answer section and selected best answer since you solved your own question.
To answer your follow up question, see the following python:
>>> int("xyz")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'xyz'
>>>
As you can see from that, when you try to convert a non numeric value, a ValueError is raised and not a TypeError.
You could have passed the challenge if you had caught a ValueError. But in this case, the challenge only requires that you catch the exceptions.
markmneimneh
14,132 PointsHello
Because it should be ValueError and not TypeError
Greg Isaacson
Courses Plus Student 702 PointsOk thanks...I thought ValueError was when you cant mix an integer and a string...
I guess thats a TypeError...wont make that mistake again:D
Greg Isaacson
Courses Plus Student 702 PointsGreg Isaacson
Courses Plus Student 702 Points