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 trial

Python Python Basics (2015) Number Game App Squared

Greg Isaacson
PLUS
Greg Isaacson
Courses Plus Student 702 Points

A 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!

squared.py
# 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) 
Greg Isaacson
Greg Isaacson
Courses Plus Student 702 Points
def squared(num):
    try:
        num = int(num)
        return num ** 2
    except TypeError:
        return len(num) * num 
          ```

I tried this and it is not passing...where am I going wrong:?:/

3 Answers

Greg Isaacson
PLUS
Greg Isaacson
Courses Plus Student 702 Points

I got it working by REMOVING TYPE ERROR.

Why did TypeError make it not work???

Hi 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.

Hello

Because it should be ValueError and not TypeError

Greg Isaacson
PLUS
Greg Isaacson
Courses Plus Student 702 Points

Ok 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