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

Richard Farr
Richard Farr
4,857 Points

I don't see why this function does not return the correct answer

squared(5) should return 25 but does not... why?

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

def squared(i):
    if type(i) == int:
        int1 = int(i)
        output1 = (int1 ** 2)
        return output1
    else:
        input_length = len(i)
        output2 = (i * input_length)
        return output2

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

this seems fine to me for the first and third use cases, but fails the second. this is because you are not type casting until after you have determined if the input is int. since '2' is not int it goes to the else block.