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

Sean Hetzel
Sean Hetzel
1,334 Points

Tested in python ide with correct output for all cases such as 2, "2", and "two."

treehouse says "squared didn't return the right answer. I got the right answer for all other cases in IDLE. I don't understand why its not producing the correct output.

squared.py
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(arg):
    try:
        return print(int(arg) ** 2)
    except:
        return print(arg * len(arg))

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

The problem here is that you're wrapping the return values in print statements. You're printing out the correct answer, but the print function itself always returns None, so your function will only ever return None. If you remove the print statements, I think it'll work fine. That being said, it's considered bad form in Python to just generally except everything. In your case, I think you want to except ValueError in case the input can't be converted to an int