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

Omprakash Panigrahi
Omprakash Panigrahi
9,632 Points

Code works on system but not on the quiz site

The code returns the correct results when run on a python compiler in the pc but it doesnt work when i try to submit my code.

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

1 Answer

Mckenzie Hessel
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Mckenzie Hessel
Full Stack JavaScript Techdegree Graduate 20,437 Points

I just tried this in the try block and it worked for me: return (int(item) ** 2)

Because you first need to turn the item into an int if you are going to be able to square it.

If you leave it like this: return int(item ** 2)

it will do whatever is inside of the parentheses first. Which could mean trying to square a string, which would not work