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

Joel Price
Joel Price
13,711 Points

Can't seem to get squared to return the right answer. Is it my if statement?

I've worked my code through all the errors I was getting up until this last one. It seems to be close to what the Challenge Task is looking for, but off a bit. Anyone have any ideas? I think it's my if statement, but I can't figure out what I would do to fix it.

Any help is appreciated.

squared.py
# EXAMPLES
def squared(arg1):
    try:
        square = int(arg1) ** 2
        if square == True:
            return square
    except ValueError:
            return arg1 * len(arg1)
    else:
            return False
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"

1 Answer

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

you are close. your if block and the else statement are not necessary. your except statement is fine. you can fix by just returning what you are currently assigning to the square variable. this way, you cast the input if necessary and return the square of the integer. if it can't be cast to an integer, you hit the except statement and return the string repeated however many times.