Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,083 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
216,083 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
216,083 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
216,083 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).