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

Bruce Röttgers
Bruce Röttgers
18,211 Points

Question is broken

This question is broken. I tried the code out in a workspace, works on all cases (examples: "5", "max"). But I get an error in the challenge.

squared.py
# EXAMPLES
# squared(5) would return 25
# squared("2") would return 4
# squared("tim") would return "timtimtim"
def squared(num):
    try:
        val = int(num)
        print(val * val)
    except:
        length = len(num)
        print(num * length)
    else:
        print("Not an Int or String")

2 Answers

andren
andren
28,558 Points

The challenge asks you to return a number / string, not print it. Returning and printing a value might look somewhat similar in the REPL for this type of code but they actually do very different things.

If you replace the print statements with return statements like this:

def squared(num):
    try:
        val = int(num)
        return val * val
    except:
        length = len(num)
        return num * length
    else:
        return "Not an Int or String"

Then your code will work.

AJ Salmon
AJ Salmon
5,675 Points

You need to be returning the values, not printing them!