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

Hunter Gass
Hunter Gass
4,878 Points

squared objective

really struggling with this last objective. I don't know what im doing wrong any help would be much appreiated!

squared.py
def squared(x):
    try:
        x = int(x) 
    except:
        return len(x) ** 2 
    else:
        return  x * x 

print squared(5) 
print squared("3") 
print squared("boo") 
Hunter Gass
Hunter Gass
4,878 Points

so then my code here is okay?

def squared(x):
    try:
        x = int(x)
    except:
        return len(x) ** 2
    else:
        return x * x
Hunter Gass
Hunter Gass
4,878 Points

I figured it out! thank you so much Steven!

1 Answer

Steven Parker
Steven Parker
231,236 Points

You have an issue with the value you return on an exception.

The instructions say to "return the argument multiplied by its length". But your statement is returning the square of the length instead. You probably want something more like this:

        return len(x) * x 
Steven Parker
Steven Parker
231,236 Points

Well, you also need to get rid of those print statements at the bottom — they are not part of the challenge.

Hunter Gass
Hunter Gass
4,878 Points

i keep getting syntax error

Steven Parker
Steven Parker
231,236 Points

Did you change more than just the one line I showed you (plus removing the *print*s)?

Perhaps you should show your entire code as it is now.

Hunter Gass
Hunter Gass
4,878 Points
def squared(x):
    try:
        x = int(x) 
    except:
        return len(x) ** 2 
    else:
        return len(x) * x 

I really can't thank you enough for your persistence to help me lawl

Steven Parker
Steven Parker
231,236 Points

It looks like you replaced the wrong return statement. My suggested replacement was for what you return on an exception, where you currently return the square of the length.

But you put it on the line where you don't have an exception instead (that line was fine as it was).