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

I'm not sure what I'm doing wrong in this challenge?

Please don't give me the answer, just tell me what I'm doing wrong logically.

I'd still like to try my best to solve this specific challenge myself.

Any help is appreciated!

squared.py
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"

def squared(num):
    try int(num):
        return int(num) ** 2
    except ValueError:
        return num * len(num)

1 Answer

Steven Parker
Steven Parker
229,785 Points

You're really close! The "try" (and its colon) should be on a line by itself. The other stuff is already in the "return" formula and doesn't need to be done again anyway.

David Evans
David Evans
10,490 Points

To add on to this, give this video another look if you get stuck, they have a working example of the try/except: https://teamtreehouse.com/library/python-basics/number-game-app/number-game-refinement

Up voted this answer

I changed it to this, but now it says "'squared' didn't return the right answer"?

def squared(num):
    try:
        int(num) ** 2
    except ValueError:
        num * len(num)

Nvm! I fixed it, haha.

def squared(num):
    try:
        return int(num) ** 2
    except ValueError:
        return num * len(num)