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

Didier Borel
Didier Borel
2,837 Points

why is this function not working

can someone tell what is wrong here

squared.py
def squared(arguement):
    if type(arguement)==int:
        arguement**2
    else:
        len(arguement)*len(arguement)

3 Answers

Didier Borel
Didier Borel
2,837 Points

Behar, thanks for your quick response. I am tied up with something else at the moment, but I will look later. Thanks for you effort, and I will play around with your second answer and let you know

I can defintely see your idea here, but you should be using try an except, because the challenge actually wants you to square strings that can be turned into integers aswell.

So say you have squared("2") Should return 4.

Also squared(2) should return 4. So instead of checking for type, simply try to turn the aruguement into an integer, and if that can be done, square it, else multiple the length of the string by itself.

Didier Borel
Didier Borel
2,837 Points

hmm, what answer do you suggest, i don't follow?

Well i am not going to show the finished code, but i can give you some pointers. There is a function that 'trys' to to turn something into an int, it will spit a valueError if it cant, so look here.

try:
    int(arguement)
    # If it can do that it will go here
except ValueError:
    # If it cant it will go here.

So if we have:

test = "5"
test = int(test)
# Now test would be the integer 5.

Try to see if you can solve it with that, else feel free to write back!