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

I'm having trouble with the squared coding exercise in Python basics. Any help would be greatly appreciated.

I can't seem to get it to work properly.

Steven Parker
Steven Parker
243,658 Points

... and a link to the challenge itself.

https://teamtreehouse.com/library/python-basics/number-game-app/squared This is the link to the code challenge and this is what I have so far.

def squared(arg):
    try:
        return arg * arg
    except TypeError:
        if type(arg) == str:
            return int(arg)
            return len(arg) * arg

Thanks for the help, Courtney. I was kind of stuck there.

1 Answer

I think your error type is wrong. I had to play around with this for a long time, mine might be a complicated way of answering it but it worked! Mine looked like this:

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